If your Unity code requires conditional compilation symbols to be present this bit of code may come in handy. After unity compiles your scripts it executes any classes that have the InitializeOnLoad attribute. You can call the SetupConditionalCompilation method provided in the code snippet below to ensure that the conditional compilation symbols persist in your unity project. If a symbol was not present and was added it will write out a notification in the unity console.
[InitializeOnLoad]
public class GridMappingSetup
{
static GridMappingSetup()
{
var types = new[] { BuildTargetGroup.Standalone, BuildTargetGroup.WebPlayer };
var toInclude = new[] { "CBXControls", "GridMapping", "QuickTools", "ToolService", "TileMaterialCreation" };
SetupConditionalCompilation(types, toInclude);
}
}
public static void SetupConditionalCompilation(BuildTargetGroup[] platformTargets, string[] symbolsToInclude)
{
foreach (var type in platformTargets)
{
var hasEntry = new bool[symbolsToInclude.Length];
var conditionals = PlayerSettings.GetScriptingDefineSymbolsForGroup(type).Trim();
var parts = conditionals.Split(';');
var changed = false;
foreach (var part in parts)
{
for (int i = 0; i < symbolsToInclude.Length; i++)
{
if (part.Trim() == symbolsToInclude[i].Trim())
{
hasEntry[i] = true;
break;
}
}
}
for (int i = 0; i < hasEntry.Length; i++)
{
if (!hasEntry[i])
{
conditionals += (String.IsNullOrEmpty(conditionals) ? String.Empty : ";") + symbolsToInclude[i];
changed = true;
}
}
PlayerSettings.SetScriptingDefineSymbolsForGroup(type, conditionals);
if (changed)
{
Debug.Log(String.Format("Updated player conditional compilation symbols for {0}: {1}", type, conditionals));
}
}
}