i’d like to use Mono.Cecil and things like obfuscators or code contracts that modify the dlls produced by unity.
for desktop builds, i can just go into the dll and change it. this isn’t the case with web builds or iphone builds, though. and in the editor, it’d be nice to be able to do it right after compilation, so that it’s done by the time i hit play.
any ways of getting in there? i’m considering replacing unity’s bytecode stripper or something with my own program so i can hook in that way, but that seems pretty shady.
simply moving the code to a DLL that you can post process before copying to the Assets folder works.
I have a static class with [InitializeOnLoad] which processes assemblies automatically, unfortunately doing this to Assembly-Csharp.dll causes issues, see my question here
for details. Anyway, I’m hoping that now if I compile my code to a dll, and set up the visual studio project to output the dll into my Assets folder, that this approach will work for those. If it doesn’t, then it may require setting up a post compilation step in visual studio to do the processing before copying into the Assets folder.
UPDATE:
It turned out that there was a bug in my assembly processing code (I was importing a type or method into the same module that defined it, causing a cyclic reference). So it seems from my current tests that it’s fine to make changes to Assembly-CSharp.dll and then call UnityEditorInternal.InternalEditorUtility.RequestScriptReload().
I wish I had an answer, but do not. Just a vote of support.
This is also important to the organization for which I work. We need to be able to have compile time weaving for AOP (aspect oriented programming). This will need to work in the Editor as well as other players.
We spoke with JB Evain at Unite 11 about how to integrate Mono.Cecil into the build process of Unity and were given direction to look into AssetPostprocessor and BuildPipeline, but we are still not achieving success there.
You can put all of your code in a separate MSVC project and add post-build steps in there. Then automatically copy the final dll into Unity project.
If you want to be able to see the lines of Debug.* or exceptions messages you also need to convert .net pdb into mono mdb (and copy it along with your dll). There is a utility tool for it next to mono binaries. Unfortunatelly if you click the debug message it self it wont move you into the editor, did not yet find a way to hook into that.
Iam using this to apply AOP PostSharp to my Unity code.
Pros:
You can do anything with your code,
AOP, etc…
When you hit ctrl+s out of habit,
Unity won’t immediately start
recompile
Cons:
Might take a longer time to compile in MSVC and import dll into Unity, than just Unity native compile
This is a completely out of Unity solution, might be tricky or impossible to set this up with the cloud build