using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; namespace DarkTonic.MasterAudio.EditorScripts { public static class DTDefineHelper { /// /// Checks if a symbol exists in the project's Scripting Define Symbols for the current build target. /// public static bool DoesScriptingDefineSymbolExist(string symbol) { var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup).Split(';'); for (int i = 0; i < defines.Length; i++) { if (string.Equals(symbol, defines[i].Trim())) return true; } return false; } public static HashSet GetInstalledBuildTargetGroups() { var result = new HashSet(); foreach (BuildTarget target in (BuildTarget[])Enum.GetValues(typeof(BuildTarget))) { BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target); if (BuildPipeline.IsBuildTargetSupported(group, target)) { result.Add(group); } } return result; } /// /// Try to add a symbol to the project's Scripting Define Symbols for all build targets. /// public static void TryAddScriptingDefineSymbols(string symbol, bool touchFiles = false) { foreach (var group in GetInstalledBuildTargetGroups()) { try { var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group); if (!string.IsNullOrEmpty(defines)) defines += ";"; defines += symbol; PlayerSettings.SetScriptingDefineSymbolsForGroup(group, defines); } catch (Exception e) { Debug.LogException(e); } } if (touchFiles) TouchScriptsWithScriptingSymbol(symbol); RecompileScripts(); } /// /// Try to remove a symbol from the project's Scripting Define Symbols for all build targets. /// public static void TryRemoveScriptingDefineSymbols(string symbol) { foreach (var group in GetInstalledBuildTargetGroups()) { try { var symbols = new List(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';')); symbols.Remove(symbol); var defines = string.Join(";", symbols.ToArray()); PlayerSettings.SetScriptingDefineSymbolsForGroup(group, defines); } catch (Exception e) { Debug.LogException(e); } } RecompileScripts(); } /// /// Add or remove a scripting define symbol. /// public static void ToggleScriptingDefineSymbol(string define, bool value, bool touchFiles = false) { if (value == true) TryAddScriptingDefineSymbols(define, touchFiles); else TryRemoveScriptingDefineSymbols(define); } /// /// Triggers a script recompile. /// public static void RecompileScripts() { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); #if UNITY_2019_3_OR_NEWER UnityEditor.EditorUtility.RequestScriptReload(); #else UnityEditorInternal.InternalEditorUtility.RequestScriptReload(); #endif } /// /// The only reliable way to force a recompile and get the editor to recognize /// MonoBehaviour scripts and wrappers in Plugins is to actually change those /// files. :/ /// /// Touch files that cehck this scripting symbol. public static void TouchScriptsWithScriptingSymbol(string symbol) { var path = Application.dataPath + "/Plugins/Pixel Crushers/"; path = path.Replace("/", "\\"); string[] filenames = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories); var found = string.Empty; var recompileAtText = "// Recompile at " + DateTime.Now + "\r\n"; var searchString = "#if " + symbol; foreach (string filename in filenames) { var text = File.ReadAllText(filename); if (text.Contains(searchString)) { found += filename + "\n"; if (text.StartsWith("// Recompile at ")) { var lines = File.ReadAllLines(filename); lines[0] = recompileAtText; File.WriteAllLines(filename, lines); } else { text = recompileAtText + text; File.WriteAllText(filename, text); } } } //Debug.Log("Touched: " + found); } } }