mirror of
https://github.com/DCFApixels/DragonECS-Unity.git
synced 2025-09-17 17:34:34 +08:00
add DefineUtility
This commit is contained in:
parent
657f1edde6
commit
019766b2c0
51
src/DebugUtils/DefinesUtility.cs
Normal file
51
src/DebugUtils/DefinesUtility.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using UnityEditor;
|
||||||
|
|
||||||
|
namespace DCFApixels.DragonECS.Unity.Internal
|
||||||
|
{
|
||||||
|
internal static class DefinesUtility
|
||||||
|
{
|
||||||
|
public static Symbols[] 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 => new Symbols(o.Name, (bool)o.GetValue(null))).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ApplyDefines(IEnumerable<Symbols> 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
|
||||||
|
|
||||||
|
foreach (var define in defines)
|
||||||
|
{
|
||||||
|
symbolsString = symbolsString.Replace(define.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
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Symbols
|
||||||
|
{
|
||||||
|
public readonly string Name;
|
||||||
|
public bool flag;
|
||||||
|
public Symbols(string name, bool flag)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
this.flag = flag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
src/DebugUtils/DefinesUtility.cs.meta
Normal file
11
src/DebugUtils/DefinesUtility.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 488a62edebd480247b8eab1f153c237d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -1,8 +1,8 @@
|
|||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
using DCFApixels.DragonECS.Unity.Internal;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.Build;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS.Unity.Editors
|
namespace DCFApixels.DragonECS.Unity.Editors
|
||||||
@ -17,22 +17,12 @@ namespace DCFApixels.DragonECS.Unity.Editors
|
|||||||
wnd.Show();
|
wnd.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefineSymbolsInfo[] _defineSymbols = null;
|
private IEnumerable<DefinesUtility.Symbols> _defineSymbols = null;
|
||||||
private void InitDefines()
|
private void InitDefines()
|
||||||
{
|
{
|
||||||
string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.Standalone);//TODO
|
if (_defineSymbols != null) { return; }
|
||||||
_defineSymbols =
|
_defineSymbols = DefinesUtility.LoadDefines(typeof(EcsDefines));
|
||||||
typeof(EcsDefines).GetFields(BindingFlags.Static | BindingFlags.Public).Select(o => new DefineSymbolsInfo(o.Name, false))
|
_defineSymbols = _defineSymbols.Concat(DefinesUtility.LoadDefines(typeof(EcsUnityDefines)));
|
||||||
.Concat(typeof(EcsUnityDefines).GetFields(BindingFlags.Static | BindingFlags.Public).Select(o => new DefineSymbolsInfo(o.Name, false)))
|
|
||||||
.ToArray();
|
|
||||||
for (int i = 0; i < _defineSymbols.Length; i++)
|
|
||||||
{
|
|
||||||
var symbol = _defineSymbols[i];
|
|
||||||
if (symbolsString.Contains(symbol.name))
|
|
||||||
{
|
|
||||||
symbol.isOn = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private void OnGUI()
|
private void OnGUI()
|
||||||
{
|
{
|
||||||
@ -88,59 +78,25 @@ namespace DCFApixels.DragonECS.Unity.Editors
|
|||||||
rect = GUILayoutUtility.GetLastRect();
|
rect = GUILayoutUtility.GetLastRect();
|
||||||
rect.xMin += 9f;
|
rect.xMin += 9f;
|
||||||
GUI.Label(rect, "Scripting Define Symbols", EditorStyles.whiteLargeLabel);
|
GUI.Label(rect, "Scripting Define Symbols", EditorStyles.whiteLargeLabel);
|
||||||
if (_defineSymbols == null)
|
|
||||||
{
|
InitDefines();
|
||||||
InitDefines();
|
|
||||||
}
|
|
||||||
EditorGUI.BeginChangeCheck();
|
EditorGUI.BeginChangeCheck();
|
||||||
for (int i = 0; i < _defineSymbols.Length; i++)
|
foreach (var symbol in _defineSymbols)
|
||||||
{
|
{
|
||||||
GUILayout.BeginHorizontal();
|
symbol.flag = EditorGUILayout.ToggleLeft(symbol.Name, symbol.flag);
|
||||||
var symbol = _defineSymbols[i];
|
|
||||||
|
|
||||||
string text = symbol.name == "DEBUG" ? symbol.name + " (Build Only)" : symbol.name;
|
|
||||||
symbol.isOn = EditorGUILayout.ToggleLeft(text, symbol.isOn);
|
|
||||||
|
|
||||||
GUILayout.EndHorizontal();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EditorGUI.EndChangeCheck()) { }
|
if (EditorGUI.EndChangeCheck()) { }
|
||||||
if (GUILayout.Button("Apply"))
|
if (GUILayout.Button("Apply"))
|
||||||
{
|
{
|
||||||
BuildTargetGroup group = EditorUserBuildSettings.selectedBuildTargetGroup;
|
DefinesUtility.ApplyDefines(_defineSymbols);
|
||||||
#if UNITY_6000_0_OR_NEWER
|
|
||||||
string symbolsString = PlayerSettings.GetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group));
|
|
||||||
for (int i = 0; i < _defineSymbols.Length; i++)
|
|
||||||
{
|
|
||||||
symbolsString = symbolsString.Replace(_defineSymbols[i].name, "");
|
|
||||||
}
|
|
||||||
symbolsString += ";" + string.Join(';', _defineSymbols.Where(o => o.isOn).Select(o => o.name));
|
|
||||||
PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(group), symbolsString);
|
|
||||||
#else
|
|
||||||
string symbolsString = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
|
|
||||||
for (int i = 0; i < _defineSymbols.Length; i++)
|
|
||||||
{
|
|
||||||
symbolsString = symbolsString.Replace(_defineSymbols[i].name, "");
|
|
||||||
}
|
|
||||||
symbolsString += ";" + string.Join(';', _defineSymbols.Where(o => o.isOn).Select(o => o.name));
|
|
||||||
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, symbolsString);
|
|
||||||
#endif
|
|
||||||
InitDefines();
|
InitDefines();
|
||||||
}
|
}
|
||||||
GUILayout.EndVertical();
|
GUILayout.EndVertical();
|
||||||
|
|
||||||
EditorGUIUtility.labelWidth = labelWidth;
|
EditorGUIUtility.labelWidth = labelWidth;
|
||||||
}
|
}
|
||||||
private class DefineSymbolsInfo
|
|
||||||
{
|
|
||||||
public string name;
|
|
||||||
public bool isOn;
|
|
||||||
public DefineSymbolsInfo(string name, bool isOn)
|
|
||||||
{
|
|
||||||
this.name = name;
|
|
||||||
this.isOn = isOn;
|
|
||||||
}
|
|
||||||
public static implicit operator DefineSymbolsInfo(string a) => new DefineSymbolsInfo(a, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user