Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Change assembly compile order to compile proto auto-generated code before the rest

Discussion in 'Scripting' started by Suduckgames, Jun 3, 2022.

  1. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    210
    Hi there! We have a very big project and we are implementing protobuf for communication with an external app.

    We have a system that download the proto client for your machine, download the definitions and generate the code necessary for that protos.

    We are not pushing the generated code to our repo so we need to do that step at compilation time.

    This work great when we are switching branches. However, when you open the project the first time since unity enters in Safe mode ( because the auto-generated code hasn't been generated, so the classes that points to that code gives errors because the classes doesn't exist yet) , it doesn't compile so it doesn't gives any callback.

    So I have no way to detect that and generated the classes before compiles those assemblies.

    Is any way to have a callback before the compilation or when entering safe mode or some way to overcome this?

    I am using this code


    Code (CSharp):
    1.    [InitializeOnLoad]
    2.     public class ProtobufEditor : AssetPostprocessor
    3.     {
    4.         static ProtobufEditor()
    5.         {
    6.             CompilationPipeline.compilationStarted += OnProjectCompile;
    7.             CompilationPipeline.assemblyCompilationStarted += OnProjectCompile;
    8.             OnProjectCompile();
    9.         }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    6,692
    You want a script that will execute before compilation when starting. That's always going to be difficult because you will need the script to still be compiled first which means the project first needs to compile without errors.
    Why don't you push the code to your repro? Could you add some empty code that represents protobuf, that at least lets it compile and then swap that out for the real thing after startup?

    Alternatively, you could do something like:

    Code (csharp):
    1. #if PROTOBUF
    2. // some code that needs protobuf
    3. #endif
    Keep all of the code that needs the missing assembly behind the #if so that you can still compile.
    Now set this define in the player settings after you have imported protobuf.
     
    Suduckgames likes this.
  3. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    210
    Thank you very much for your answer!

    We will end pushing the code to the repo.
    Usually, push generated code to repo is not a good practice but I think that in this case it will work correctly.

    The empty code can do the trick but I think it is an overengineer option and the define will mesh up all the code.
     
unityunity