- Home /
Set preprocessor symbols from Editor script
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!
Answer by Brian@Artific · Jul 25, 2013 at 04:41 AM
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)
@$$anonymous$$@Floppy That doesn't answer the question. Please at least read the title before answering :)
Ugh ... quite right - no more insomnia-fueled answers for me. :(
See my edit above for an answer to your actual question rather than random semi-related facts.
Answer by hasanbayat · Oct 21, 2017 at 12:02 PM
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 () ) );
}
}
Great answer! Deserves more credit and I would flag it as the more correct answer as it's safer and complete.
Great solution. I added your code as a private class in my custom editor script.
/// <summary>
/// Adds the given define symbols to PlayerSettings define symbols.
/// Just add your own define symbols to the Symbols property at the below.
/// </summary>
private static class AddDefineSymbols
{
/// <summary>
/// Symbols that will be added to the editor
/// </summary>
private static List<string> Symbols = new List<string>();
/// <summary>
/// Add define symbols as soon as Unity gets done compiling.
/// </summary>
public static void Add(string define)
{
Symbols.Add(define);
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()));
}
/// <summary>
/// Clear all symbols.
/// </summary>
public static void Clear()
{
Symbols.Clear();
}
}