Search Unity

Question Apply/Remove Define Symbols before script compilation

Discussion in 'Scripting' started by EduardDraude, Apr 16, 2021.

  1. EduardDraude

    EduardDraude

    Joined:
    Mar 8, 2019
    Posts:
    60
    Hey!

    So I want to add and remove define symbols depending on installed packages. E.g. I have an static class with the attribute
    [InitializeOnLoad}
    So it will be called when unity recompiles code. In this method I just check the project manifest if it has an entry (in my case the com.unity.addressable package). If it is present I want to add a define symbol and if it is not present it should be removed (if the define symbol is present) The adding works as expected, but when the define symbol was already added and I remove the addressable package from my project, my other scripts throw an error, because in some parts I use Addressables specific code which is sourrounded by the define symbol. So my other code which should remove the define symbol is not called, because there are compilation errors.

    My question is: How can I make sure the code which add/removes define symbols runs before script compilation of the other code? Or how would one implement code which (optionally just when it is imported) uses an package API?

    Thankful for any hints :)
     
  2. Things to investigate:

    - before compilation: https://docs.unity3d.com/ScriptReference/Compilation.CompilationPipeline-compilationStarted.html
    - package manager: https://docs.unity3d.com/Packages/c...ckageManager.UI.IPackageManagerExtension.html

    I have never implemented any of these. But you can test them with a couple lines of code, I guess.
     
    EduardDraude likes this.
  3. EduardDraude

    EduardDraude

    Joined:
    Mar 8, 2019
    Posts:
    60