You could create an editor script which will set the values OnEnable() when scene will be opened.
You need to do:
Create an empty GameObject
Create a Monobehaviour script and add it to the empty GameObject ( I called it InitTest )
Create another script with extension Editor ( I called it InitTestEditor )
Add following code to the InitTestEditor, it doesn’t need to be attached to anything
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor (typeof(InitTest))]
public class InitTestEditor : Editor {
void OnEnable(){
Debug.LogWarning("<b>TESTING</b> ADDED TO Scripting Define Symbols in <b>Player Settings</b>");
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "TESTING");
}
}
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 (!!!).
using System;
using UnityEngine;
using UnityEditor;
namespace Library
{
[InitializeOnLoad]
public class Library
{
const string define = "LIBRARY_IS_AVAILABLE";
static Library()
{ AddLibrayDefineIfNeeded(); }
static void AddLibrayDefineIfNeeded()
{
// Get defines.
BuildTargetGroup buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
// Append only if not defined already.
if (defines.Contains(define))
{
Debug.LogWarning("Selected build target ("+EditorUserBuildSettings.activeBuildTarget.ToString()+") already contains <b>"+define+"</b> <i>Scripting Define Symbol</i>.");
return;
}
// Append.
PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, (defines + ";" + define));
Debug.LogWarning("<b>"+define+"</b> added to <i>Scripting Define Symbols</i> for selected build target ("+EditorUserBuildSettings.activeBuildTarget.ToString()+").");
}
}
}
All done, now check Player Settings, The symbols added
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Adds the given define symbols to PlayerSettings define symbols.
/// Just add your own define symbols to the Symbols property at the below.
/// </summary>
[InitializeOnLoad]
public class AddDefineSymbols : Editor
{
/// <summary>
/// Symbols that will be added to the editor
/// </summary>
public static readonly string [] Symbols = new string[] {
"MYCOMPANY",
"MYCOMPANY_MYPACKAGE"
};
/// <summary>
/// Add define symbols as soon as Unity gets done compiling.
/// </summary>
static AddDefineSymbols ()
{
string definesString = PlayerSettings.GetScriptingDefineSymbolsForGroup ( EditorUserBuildSettings.selectedBuildTargetGroup );
List<string> allDefines = definesString.Split ( ';' ).ToList ();
allDefines.AddRange ( Symbols.Except ( allDefines ) );
PlayerSettings.SetScriptingDefineSymbolsForGroup (
EditorUserBuildSettings.selectedBuildTargetGroup,
string.Join ( ";", allDefines.ToArray () ) );
}
}
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.
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android,"ONE");
Debug.Log("Set symbol " + PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android));
Debug.Log("Application compiled");
#if One
Debug.Log("One is defined");
#elif Ten
Debug.Log("Ten is defined");
#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.
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.