Home > Mobile >  Unity script preprocessing before compile or load with Roslyn Syntax Rewriter?
Unity script preprocessing before compile or load with Roslyn Syntax Rewriter?

Time:03-20

I'm looking for ways to "rewrite/tweak" C#-scripts in a Unity3d-project somewhere between me editing them in VS and the compilation before running/building the project in the Unity editor. I have seen that Roslyn Source Generators is possible to integrate in Unity, but I much much rather use a Roslyn Syntax Rewriter, if I could only find a way. It does not seem to be possible to integrate syntax rewriters in a normal .NET build in VS (it would have been awesome if it did though) but I was thinking that perhaps Unity have some hooks I could use instead? Perhaps when the script-resources are reloaded after being edited? I don't want to make the changes permanent though, but only visible for the build process. Any other hooks or hacks I could use here? If I only could get the script-text and transform it a little bit before the build... I would be so happy...

CodePudding user response:

I think you are looking for IPreprocessBuildWithReport.OnPreprocessBuild which allows you to run anything just before a build starts.

Note though that if you modify any files in there it would probably still be permanent.

I think you can easily prevent this from happening by using version control and just reset after building ;)

Alternatively you could of course somehow store your changes (e.g. in copied temporary files) and revert in IPostprocessBuildWithReport.OnPostprocessBuild.

Or if you know what you are doing you could rather modify the precompiled assemblies in IIl2CppProcessor.OnBeforeConvertRun which wouldn't affect your c# source code files but rather only the built managed assemblies right before they are transcripted into c

  • Related