Search Unity

  1. Unity 2019.1 is now released.
    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:
    206
    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:
    206
    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. }
     
    ilmario likes this.
  6. bluemike

    bluemike

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

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    604
    Malbers likes this.
  8. eppz

    eppz

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

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    140
  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:
    538
    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. DuskLight-Studios

    DuskLight-Studios

    Joined:
    Jan 2, 2016
    Posts:
    11
    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:
    3,320
    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.