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
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

PreprocessBuild add script define

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

  1. Gorki12

    Gorki12

    Joined:
    Jul 12, 2015
    Posts:
    71
    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.
     
unityunity