I’m working on a very simple build utility and I want to programmatically add/remove preprocessor symbols before builds.
For example, if I have Build Android-Release button, I want to programmatically add a RELEASE preprocessor to the build settings before I start the build. Or if I wanted to make a device-specific build for OUYA, I could add an appropriate symbol instead of managing that manually in the Editor.
I have found EditorUserBuildSettings.activeScriptCompilationDefines but is read-only. Any suggestions?
Thanks!
Set your preprocessor definitions in: Player Settings → Per-platform Settings → Other Settings → Configuration → Scripting Define Symbols.
EDIT: If you have multiple definitions, separate with a semicolon.
EDIT: From an editor script, use PlayerSettings.SetScriptingDefineSymbolsForGroup to set these same values programmatically, which is what I would have said if I had exercised the slightest bit of reading comprehension the first time. (apologies)
You can use AddDefineSymbols to add define symbols automatically.
Features
- Multiple Define Symbols
- Safety
- Runs when Compile ends
- Removes Duplicates
Installation
-
Download the Script or Copy/Paste it from the Below
- Open Script
- Go to Symbols property and add your own symbols
- Go back to Unity and wait for compile ends
- All done, now check Player Settings, The symbols added
Here is the code for copy/pasting:
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 () ) );
}
}