Skip to content

Instantly share code, notes, and snippets.

@forestrf
Forked from LaneF/DefineSymbolUtil.cs
Last active July 8, 2023 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forestrf/c7f71b09c3b52e7d5bf4a4a92074b135 to your computer and use it in GitHub Desktop.
Save forestrf/c7f71b09c3b52e7d5bf4a4a92074b135 to your computer and use it in GitHub Desktop.
Unity Define Symbols Utility
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