-
-
Save forestrf/c7f71b09c3b52e7d5bf4a4a92074b135 to your computer and use it in GitHub Desktop.
Unity Define Symbols Utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
using System.Linq; | |
using UnityEditor.Callbacks; | |
namespace Ashkatchap { | |
public static class DefineSymbolUtil { | |
static readonly (string, string)[] TypeAndSymbol = new[] { | |
("UltEvents.UltEventBase, UltEvents, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "USE_ULTEVENTS"), | |
}; | |
[DidReloadScripts] | |
static void Verify() { | |
foreach (var elem in TypeAndSymbol) Verify(elem.Item1, elem.Item2); | |
} | |
[InitializeOnLoadMethod] | |
static void OnInitialized() { | |
//AssemblyReloadEvents.afterAssemblyReload += Verify; | |
Application.logMessageReceived += (a, b, c) => { | |
if (c == LogType.Error && a.Contains("CS0246")) { // Type not found error. | |
// Continually repeats recompilation if there's an error CS0246 inside code surrounded by #if "symbos in TypeAndSymbol" and the Type exists. | |
foreach (var elem in TypeAndSymbol) RemoveDefine(elem.Item2); | |
} | |
}; | |
} | |
public static void Verify(string typeName, string symbol) { | |
bool wantSymbol = System.Type.GetType(typeName) != null; | |
bool hasSymbol = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup).Split(';').Contains(symbol); | |
if (wantSymbol != hasSymbol) { | |
if (wantSymbol) AddDefine(symbol); | |
else RemoveDefine(symbol); | |
} | |
} | |
public static void AddDefine(string def) { | |
Debug.Log($"Adding definition '{def}'."); | |
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, | |
PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup) + (";" + def + ";")); | |
} | |
public static void RemoveDefine(string def) { | |
Debug.Log($"Removing definition '{def}'."); | |
PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, | |
string.Join(";", PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup).Split(';').Where(d => d != def))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment