Search Unity

Feedback Set a #define / Compiler Directive when in Debug or Release Script Compilation Optimization Modes

Discussion in 'Scripting' started by Schodemeiss, Jan 25, 2021.

  1. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    I've got an interesting use case for when I set Unity in Debug or Release Script Compilation Code Optimization modes. I know I can check it at runtime using:
    Code (CSharp):
    1. UnityEditor.Compilation.CompilationPipeline.codeOptimization
    But it'd be great if changing modes set a compilation define like UNITY_EDITOR, but in this case perhaps something like:

    "CODE_OPTIMIZATION_DEBUG" and / or "CODE_OPTIMIZATION_RELEASE"

    I assume it doesn't already set a define already that I'm yet to come across and is this impossible to add to the editor?
     
  2. Schodemeiss

    Schodemeiss

    Joined:
    Jan 4, 2018
    Posts:
    43
    Given the new Beta and Alpha versions of Unity still don't provide symbols for this use case, I created a package which automatically sets some symbols related to the 'CompilationPipeline.codeOptimization' mode set in the Editor.

    GitHub: https://github.com/AlkimeeGames/CodeOptimizationSymbols
    Also available on OpenUPM: https://openupm.com/packages/com.alkimeegames.codeoptimizationsymbols/

    Essentially, when the following modes are active in the editor, the tool automatically sets the following symbols:
    • Debug Mode introduces the "UNITY_EDITOR_DEBUG" symbol.
    • Release Mode introduces the "UNITY_EDITOR_RELEASE" symbol.
    It does this when the Editor loads, when switching platforms - and removes the symbols during builds and when you exit the editor. It's careful not to remove any previously set symbols and doesn't interfere with any of built-in Unity symbols.

    I'll also create an AssetStore asset if anyone is interested.