Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scripting Define Symbols - Access in code?

Discussion in 'Scripting' started by karljj1, Mar 17, 2013.

  1. karljj1

    karljj1

    Joined:
    Feb 17, 2011
    Posts:
    440
    ocimum likes this.
  2. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    Did you ever find an answer for this?

    It would be great for setting up build scripts...
     
  3. leo-dom

    leo-dom

    Joined:
    Jun 14, 2013
    Posts:
    2
    ocimum likes this.
  4. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    Thanks! That looks to be exactly what I wanted.
     
  5. ocimum

    ocimum

    Joined:
    Apr 19, 2015
    Posts:
    12
    You could create an editor script which will set the values OnEnable() when scene will be opened.

    You need to do:

    1. Create an empty GameObject
    2. Create a Monobehaviour script and add it to the empty GameObject ( I called it InitTest )
    3. Create another script with extension Editor ( I called it InitTestEditor )
    4. Add following code to the InitTestEditor, it doesn't need to be attached to anything

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. [CustomEditor (typeof(InitTest))]
    7. public class InitTestEditor : Editor {
    8.  
    9.     void OnEnable(){
    10.         Debug.LogWarning("<b>TESTING</b> ADDED TO Scripting Define Symbols in <b>Player Settings</b>");
    11.         PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "TESTING");
    12.     }
    13.  
    14. }
     
    ROBYER1, dmyehe2016 and ilmario like this.
  6. bluemike

    bluemike

    Joined:
    Oct 26, 2015
    Posts:
    18
  7. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
    ROBYER1 and Malbers like this.
  8. eppz

    eppz

    Joined:
    Aug 2, 2014
    Posts:
    172
    This actually wipes out any Scripting Define Symbols previously declared for the given platform.
     
    TokyoWarfareProject likes this.
  9. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
  10. eppz

    eppz

    Joined:
    Aug 2, 2014
    Posts:
    172
    I'm using this below (to indicate to other scripts that my library is present).
    This can be used safely on any platform (!), also it preserves other defines (!!!).

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5.  
    6. namespace Library
    7. {
    8.  
    9.  
    10.     [InitializeOnLoad]
    11.     public class Library
    12.     {
    13.  
    14.  
    15.         const string define = "LIBRARY_IS_AVAILABLE";
    16.  
    17.  
    18.         static Library()
    19.         { AddLibrayDefineIfNeeded(); }
    20.  
    21.         static void AddLibrayDefineIfNeeded()
    22.         {
    23.             // Get defines.
    24.             BuildTargetGroup buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
    25.             string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
    26.  
    27.             // Append only if not defined already.
    28.             if (defines.Contains(define))
    29.             {
    30.                 Debug.LogWarning("Selected build target ("+EditorUserBuildSettings.activeBuildTarget.ToString()+") already contains <b>"+define+"</b> <i>Scripting Define Symbol</i>.");            
    31.                 return;
    32.             }
    33.  
    34.             // Append.
    35.             PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, (defines + ";" + define));
    36.             Debug.LogWarning("<b>"+define+"</b> added to <i>Scripting Define Symbols</i> for selected build target ("+EditorUserBuildSettings.activeBuildTarget.ToString()+").");
    37.         }
    38.     }
    39. }
     
  11. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    629
    Here is an advanced version, Just for fun. :p
    Features
    • Multiple Define Symbols
    • Safety
    • Runs when Compile ends
    • Removes Duplicates
    Installation
    1. Download the Script or Copy/Paste it from the Below
    2. Open Script
    3. Go to Symbols property and add your own symbols
    4. Go back to Unity and wait for compile ends
    5. All done, now check Player Settings, The symbols added
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. /// <summary>
    8. /// Adds the given define symbols to PlayerSettings define symbols.
    9. /// Just add your own define symbols to the Symbols property at the below.
    10. /// </summary>
    11. [InitializeOnLoad]
    12. public class AddDefineSymbols : Editor
    13. {
    14.  
    15.     /// <summary>
    16.     /// Symbols that will be added to the editor
    17.     /// </summary>
    18.     public static readonly string [] Symbols = new string[] {
    19.         "MYCOMPANY",
    20.         "MYCOMPANY_MYPACKAGE"
    21.     };
    22.  
    23.     /// <summary>
    24.     /// Add define symbols as soon as Unity gets done compiling.
    25.     /// </summary>
    26.     static AddDefineSymbols ()
    27.     {
    28.         string definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup ( EditorUserBuildSettings.selectedBuildTargetGroup );
    29.         List<string> allDefines = definesString.Split ( ';' ).ToList ();
    30.         allDefines.AddRange ( Symbols.Except ( allDefines ) );
    31.         PlayerSettings.SetScriptingDefineSymbolsForGroup (
    32.             EditorUserBuildSettings.selectedBuildTargetGroup,
    33.             string.Join ( ";", allDefines.ToArray () ) );
    34.     }
    35.  
    36. }
    Thanks.
     
  12. Eric-van-Gastel

    Eric-van-Gastel

    Joined:
    Jan 2, 2016
    Posts:
    21
    It seems there is a limit to how many symbols you can add. If you add too many, the last few are ignored by the preprocessor, and your code won't compile. I'm in a situation where I need a lot of scripting define symbols, but haven't found a solution yet.

    Did anyone encounter this before?
     
  13. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,223
    Could you file a bug report?
    Could you file a bug report? We should at least throw an error if the defines are going to be ignored.
     
    unity_T1WL1huamuCWSQ likes this.
  14. unity_T1WL1huamuCWSQ

    unity_T1WL1huamuCWSQ

    Joined:
    Jun 18, 2019
    Posts:
    10
    Why it is not working in this case?

    Code (CSharp):
    1.        
    2.        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android,"ONE");
    3.        Debug.Log("Set symbol " + PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android));
    4.        Debug.Log("Application compiled");
    5. #if One
    6.         Debug.Log("One is defined");
    7. #elif Ten
    8.         Debug.Log("Ten is defined");
    9. #endif
    I am calling it from cmd
    In the first call, it is not printing One is defined
    when I am calling it again from cmd it is printing then.
    Tried CompilerPipeLine.compilationFinished too.
     
  15. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    Defines (technically conditional compilation symbols) are processed when the code is compiled - so you can't set one and then expect it to be active on the next line, you need to wait for the code to be compiled again.