Search Unity

PreprocessBuild add script define

Discussion in 'Scripting' started by Gorki12, Aug 26, 2022.

  1. Gorki12

    Gorki12

    Joined:
    Jul 12, 2015
    Posts:
    73
    I have written my method which adds a new script define, I call it in the preprocess build script and for the test after the adding method there is a fragment marked compile directive with Debug.Log, unfortunately this code does not execute, only during the second build it executes. Is it possible to refresh the code after adding the script define in the preprocess script?

    Code:
    Code (CSharp):
    1. public class PreProcessTest : IPreprocessBuildWithReport
    2. {
    3.     public int callbackOrder { get; }
    4.     public void OnPreprocessBuild(BuildReport report)
    5.     {
    6.         AddScriptDefine("TEST_SCRIPT_DEFINE");
    7.        
    8.         #if TEST_SCRIPT_DEFINE
    9.  
    10.         Debug.Log("Test!");
    11.         #endif
    12.        
    13.         Debug.Log("End");
    14.     }
    15.  
    16.     private static void AddScriptDefine(string scriptDefine)
    17.     {
    18.         PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, out string[] defines);
    19.  
    20.         string[] newDefines = new string[defines.Length + 1];
    21.  
    22.         for (int i = 0; i < defines.Length; i++)
    23.             newDefines[i] = defines[i];
    24.  
    25.         newDefines[newDefines.Length - 1] = scriptDefine;
    26.  
    27.         PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, newDefines);
    28.     }
    29.  
     
  2. Franco_Voisard

    Franco_Voisard

    Joined:
    Apr 30, 2018
    Posts:
    20
    That is not going to work because of how defines work.

    After adding a define the code needs to be recompiled, so the first time the preprocessor is executed, the define is not there, BUT, after that first execution, the reloaded assemblies will have the defines.

    Check this if you have any doubt.