Search Unity

Mono cecil on build

Discussion in 'Scripting' started by Miramana, Dec 11, 2018.

  1. Miramana

    Miramana

    Joined:
    Oct 15, 2017
    Posts:
    8
    Hey,
    Mono cecil works fine in the editor.
    I'm changing the:
    /Library/ScriptAssemblies/Assembly-CSharp.dll
    on [InitializeOnLoad]

    and using
    EditorApplication.LockReloadAssemblies();
    EditorApplication.UnlockReloadAssemblies();
    while changing it

    The problem is when building it out, it doesn't seem to work
    I have tried:
    [PostProcessBuildAttribute(0)]

    Question is, which DLL do I process for the build. And at what stage?

    The building code is also in the Assembly-CSharp.dll if that makes a difference.
    It's also wrapped in #if UNITY_EDITOR

    help would be very much appreciated :)
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    I haven't done a lot of modifying the same assembly that the code doing the modifying is in...

    but, I will note you said:
    Well, that UNITY_EDITOR compiler directive tells it to only run in the editor. So it would make sense if it worked in the editor and not in the build.
     
  3. Miramana

    Miramana

    Joined:
    Oct 15, 2017
    Posts:
    8
    Yeah but the code is only supposed to run in the editor, it's not supposed to be compiled out. Since Cecil is modifying the build DLL from the editor.

    And trying to build the game with any code that uses UnityEditor will cause a compile error.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    I'm not following exactly...

    You're hoping to use cecil to modify the build dll when you build the game? So that the distributed package has these changes done by cecil? But this gets done from within the editor?

    When/where does this code using cecil get ran?

    ...

    The only logical place I can see doing something like this would be in a build script (so editor only specifically). Instead of using the standard build process in the file menu, you instead write one in an editor script and use the 'BuildPipeline':
    https://docs.unity3d.com/ScriptReference/BuildPipeline.html

    Then when done, you load up the generated dll and modify it.

    ...

    Otherwise, when and where would it be done?