From d0ac5a165ea03dc5bae07bf76bbb66407f6d40a2 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Wed, 12 Mar 2025 22:33:58 +0800 Subject: [PATCH] update define symbols --- Editor/Settings/DebugXSettings.cs | 126 +++++++++++++++---------- Editor/Settings/DefinesUtility.cs | 40 ++++++++ Editor/Settings/DefinesUtility.cs.meta | 2 + Runtime/Consts.cs | 6 +- 4 files changed, 119 insertions(+), 55 deletions(-) create mode 100644 Editor/Settings/DefinesUtility.cs create mode 100644 Editor/Settings/DefinesUtility.cs.meta diff --git a/Editor/Settings/DebugXSettings.cs b/Editor/Settings/DebugXSettings.cs index 690ab4b..4d595e0 100644 --- a/Editor/Settings/DebugXSettings.cs +++ b/Editor/Settings/DebugXSettings.cs @@ -1,6 +1,5 @@ #if UNITY_EDITOR using UnityEditor; -using UnityEditor.Build; using UnityEditor.Compilation; using UnityEngine; @@ -13,22 +12,24 @@ namespace DCFApixels.DebugXCore.Internal { DebugXSettings window = (DebugXSettings)EditorWindow.GetWindow(typeof(DebugXSettings)); window.Show(); - window._isHasDisableDebugXInBuildSymbols = null; + //window._isHasDisableDebugXInBuildSymbols = null; CompilationPipeline.compilationFinished -= CompilationPipeline_compilationFinished; CompilationPipeline.compilationFinished += CompilationPipeline_compilationFinished; } private static void CompilationPipeline_compilationFinished(object obj) { - _isCompilation = false; + //_isCompilation = false; + _defines = null; } - private static bool _isCompilation; - private bool? _isHasDisableDebugXInBuildSymbols = false; - private const string DEFINE_NAME = nameof(DebugXDefines.DEBUGX_DISABLE_INBUILD); - + //private static bool _isCompilation; + //private bool? _isHasDisableDebugXInBuildSymbols = false; + //private const string DEFINE_NAME = nameof(DebugXDefines.DEBUGX_DISABLE_INBUILD); + private static (string name, bool flag)[] _defines = null; private void OnGUI() { + float tmpValue; DebugX.GlobalTimeScale = EditorGUILayout.FloatField("TimeScale", DebugX.GlobalTimeScale); @@ -53,55 +54,76 @@ namespace DCFApixels.DebugXCore.Internal DebugX.GlobalColor = color; - if (_isCompilation == false) + GUILayout.BeginVertical(EditorStyles.helpBox); + GUILayout.Label("Scripting Define Symbols", EditorStyles.helpBox); + if (_defines == null) { - if (_isHasDisableDebugXInBuildSymbols == null) - { - BuildTargetGroup group = EditorUserBuildSettings.selectedBuildTargetGroup; -#if UNITY_6000_0_OR_NEWER - string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group)); -#else - string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(group); -#endif - _isHasDisableDebugXInBuildSymbols = symbolsString.Contains(DEFINE_NAME); - } - - EditorGUI.BeginChangeCheck(); - _isHasDisableDebugXInBuildSymbols = !EditorGUILayout.ToggleLeft("Show Gizmos in Build", !_isHasDisableDebugXInBuildSymbols.Value); - - if (EditorGUI.EndChangeCheck()) - { - BuildTargetGroup group = EditorUserBuildSettings.selectedBuildTargetGroup; -#if UNITY_6000_0_OR_NEWER - string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group)); -#else - string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(group); -#endif - if (symbolsString.Contains(DEFINE_NAME) == false) - { - symbolsString = symbolsString + ", " + DEFINE_NAME; - } - else - { - symbolsString = symbolsString.Replace(DEFINE_NAME, ""); - - } - -#if UNITY_6000_0_OR_NEWER - PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group), symbolsString); -#else - PlayerSettings.SetScriptingDefineSymbolsForGroup(group, symbolsString); -#endif - _isCompilation = true; - } + _defines = DefinesUtility.LoadDefines(typeof(DebugXDefines)); } - else + for (int i = 0; i < _defines.Length; i++) { - _isHasDisableDebugXInBuildSymbols = null; - GUI.enabled = false; - EditorGUILayout.ToggleLeft("Show Gizmos in Build (Locked for compilation)", false); - GUI.enabled = true; + ref var define = ref _defines[i]; + define.flag = EditorGUILayout.ToggleLeft(define.name, define.flag); } + if (GUILayout.Button("Apply Defines")) + { + DefinesUtility.ApplyDefines(_defines); + } + GUILayout.EndVertical(); + + // if (_isCompilation == false) + // { + // if (_isHasDisableDebugXInBuildSymbols == null) + // { + // BuildTargetGroup group = EditorUserBuildSettings.selectedBuildTargetGroup; + //#if UNITY_6000_0_OR_NEWER + // string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group)); + //#else + // string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(group); + //#endif + // _isHasDisableDebugXInBuildSymbols = symbolsString.Contains(DEFINE_NAME); + // } + // + // EditorGUI.BeginChangeCheck(); + // _isHasDisableDebugXInBuildSymbols = !EditorGUILayout.ToggleLeft("Show Gizmos in Build", !_isHasDisableDebugXInBuildSymbols.Value); + // + // + // + // + // + // if (EditorGUI.EndChangeCheck()) + // { + // BuildTargetGroup group = EditorUserBuildSettings.selectedBuildTargetGroup; + //#if UNITY_6000_0_OR_NEWER + // string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group)); + //#else + // string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(group); + //#endif + // if (symbolsString.Contains(DEFINE_NAME) == false) + // { + // symbolsString = symbolsString + ", " + DEFINE_NAME; + // } + // else + // { + // symbolsString = symbolsString.Replace(DEFINE_NAME, ""); + // + // } + // + //#if UNITY_6000_0_OR_NEWER + // PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group), symbolsString); + //#else + // PlayerSettings.SetScriptingDefineSymbolsForGroup(group, symbolsString); + //#endif + // _isCompilation = true; + // } + // } + // else + // { + // _isHasDisableDebugXInBuildSymbols = null; + // GUI.enabled = false; + // EditorGUILayout.ToggleLeft("Show Gizmos in Build (Locked for compilation)", false); + // GUI.enabled = true; + // } diff --git a/Editor/Settings/DefinesUtility.cs b/Editor/Settings/DefinesUtility.cs new file mode 100644 index 0000000..bb1dbaf --- /dev/null +++ b/Editor/Settings/DefinesUtility.cs @@ -0,0 +1,40 @@ +using System; +using System.Linq; +using System.Reflection; +using UnityEditor; +using UnityEditor.Build; + +namespace DCFApixels.DebugXCore.Internal +{ + internal static class DefinesUtility + { + public static (string name, bool flag)[] LoadDefines(Type defineConstsType) + { + const BindingFlags REFL_FLAGS = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; + var fields = defineConstsType.GetFields(REFL_FLAGS); + return fields.Where(o => o.FieldType == typeof(bool)).Select(o => (o.Name, (bool)o.GetValue(null))).ToArray(); + + } + + public static void ApplyDefines((string name, bool flag)[] defines) + { + BuildTargetGroup group = EditorUserBuildSettings.selectedBuildTargetGroup; +#if UNITY_6000_0_OR_NEWER + string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group)); +#else + string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone); +#endif + + for (int i = 0; i < defines.Length; i++) + { + symbolsString = symbolsString.Replace(defines[i].name, ""); + } + symbolsString += ";" + string.Join(';', defines.Where(o => o.flag).Select(o => o.name)); +#if UNITY_6000_0_OR_NEWER + PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group), symbolsString); +#else + PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, symbolsString); +#endif + } + } +} diff --git a/Editor/Settings/DefinesUtility.cs.meta b/Editor/Settings/DefinesUtility.cs.meta new file mode 100644 index 0000000..c15c604 --- /dev/null +++ b/Editor/Settings/DefinesUtility.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 77addf06144f4214a925257238d65cb0 \ No newline at end of file diff --git a/Runtime/Consts.cs b/Runtime/Consts.cs index f82c30c..23fb951 100644 --- a/Runtime/Consts.cs +++ b/Runtime/Consts.cs @@ -48,19 +48,19 @@ namespace DCFApixels.DebugXCore public static class DebugXDefines { public const bool DEBUGX_DISABLE_INBUILD = -#if DISABLE_DEBUGX_INBUILD +#if DEBUGX_DISABLE_INBUILD true; #else false; #endif public const bool DEBUGX_ENABLE_PHYSICS2D = -#if DISABLE_DEBUGX_INBUILD +#if DEBUGX_ENABLE_PHYSICS2D true; #else false; #endif public const bool DEBUGX_ENABLE_PHYSICS3D = -#if DISABLE_DEBUGX_INBUILD +#if DEBUGX_ENABLE_PHYSICS3D true; #else false;