gui optimization

This commit is contained in:
DCFApixels 2025-03-10 17:56:18 +08:00
parent 834651887b
commit 3d6e8d108d
13 changed files with 162 additions and 126 deletions

View File

@ -20,7 +20,7 @@ namespace DCFApixels.DragonECS
{ {
get get
{ {
if(_singletonInstance == null) if (_singletonInstance == null)
{ {
_singletonInstance = FindOrCreateSingleton(); _singletonInstance = FindOrCreateSingleton();
} }

View File

@ -10,27 +10,41 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
private EcsWorldProviderBase Target => (EcsWorldProviderBase)target; private EcsWorldProviderBase Target => (EcsWorldProviderBase)target;
private static Color _emptyColor = new Color32(255, 0, 75, 100);
private static Color _destroyedColor = new Color32(255, 75, 0, 100);
private static Color _aliveColor = new Color32(75, 255, 0, 100);
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
EcsWorld world = Target.GetCurrentWorldRaw(); EcsWorld world = Target.GetCurrentWorldRaw();
Color labelBackColor;
string labelText;
if (world == null) if (world == null)
{ {
var style = UnityEditorUtility.GetStyle(new Color32(255, 0, 75, 100)); labelBackColor = _emptyColor;
GUILayout.Box("Is Empty", style, GUILayout.ExpandWidth(true)); labelText = "Is Empty";
} }
else else
{ {
if (world.IsDestroyed) if (world.IsDestroyed)
{ {
var style = UnityEditorUtility.GetStyle(new Color32(255, 75, 0, 100)); labelBackColor = _destroyedColor;
GUILayout.Box($"{world.GetMeta().Name} ( {world.ID} ) Destroyed", style, GUILayout.ExpandWidth(true)); labelText = $"{world.GetMeta().Name} ( {world.ID} ) Destroyed";
} }
else else
{ {
var style = UnityEditorUtility.GetStyle(new Color32(75, 255, 0, 100)); labelBackColor = _aliveColor;
GUILayout.Box($"{world.GetMeta().Name} ( {world.ID} )", style, GUILayout.ExpandWidth(true)); labelText = $"{world.GetMeta().Name} ( {world.ID} )";
} }
} }
using (EcsGUI.SetBackgroundColor(labelBackColor))
{
GUILayout.Box("Is Empty", UnityEditorUtility.GetWhiteStyle(), GUILayout.ExpandWidth(true));
}
EcsGUI.Layout.DrawWorldBaseInfo(Target.GetCurrentWorldRaw()); EcsGUI.Layout.DrawWorldBaseInfo(Target.GetCurrentWorldRaw());
base.OnInspectorGUI(); base.OnInspectorGUI();

View File

@ -24,7 +24,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
var drawers = UnityEditorUtility._entityEditorBlockDrawers; var drawers = UnityEditorUtility._entityEditorBlockDrawers;
if (drawers.Length > 0) if (drawers.Length > 0)
{ {
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
bool isExpand = false; bool isExpand = false;
using (EcsGUI.CheckChanged()) using (EcsGUI.CheckChanged())

View File

@ -95,7 +95,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
GUILayout.Label("[Runners]", _headerStyle); GUILayout.Label("[Runners]", _headerStyle);
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
int i = 0; int i = 0;
foreach (var item in Target.Pipeline.AllRunners) foreach (var item in Target.Pipeline.AllRunners)
@ -112,7 +112,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (system is SystemsLayerMarkerSystem markerSystem) if (system is SystemsLayerMarkerSystem markerSystem)
{ {
GUILayout.EndVertical(); GUILayout.EndVertical();
GUILayout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f)); GUILayout.BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle());
using (EcsGUI.Layout.BeginHorizontal()) using (var scope = EcsGUI.SetAlignment(GUI.skin.label)) using (EcsGUI.Layout.BeginHorizontal()) using (var scope = EcsGUI.SetAlignment(GUI.skin.label))
{ {
@ -150,7 +150,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
Color color = EcsGUI.SelectPanelColor(meta, index, -1); Color color = EcsGUI.SelectPanelColor(meta, index, -1);
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(color, 0.2f))) using (EcsGUI.Layout.BeginVertical(color.SetAlpha(0.2f)))
{ {
if (IsShowInterfaces) if (IsShowInterfaces)
{ {
@ -170,7 +170,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
Color color = EcsGUI.SelectPanelColor(meta, index, -1); Color color = EcsGUI.SelectPanelColor(meta, index, -1);
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(color, 0.2f))) using (EcsGUI.Layout.BeginVertical(color.SetAlpha(0.2f)))
{ {
GUILayout.Label(meta.Name, EditorStyles.boldLabel); GUILayout.Label(meta.Name, EditorStyles.boldLabel);
GUILayout.Label(string.Join(", ", runner.ProcessRaw.Cast<object>().Select(o => o.GetType().Name)), systemsListStyle); GUILayout.Label(string.Join(", ", runner.ProcessRaw.Cast<object>().Select(o => o.GetType().Name)), systemsListStyle);

View File

@ -146,7 +146,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
//GUILayout.Space(10f); //GUILayout.Space(10f);
//using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(GetGenericPanelColor(index)))) //using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(GetGenericPanelColor(index))))
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
var mask = executor.Mask; var mask = executor.Mask;
DrawConstraint("+", mask.Incs); DrawConstraint("+", mask.Incs);
@ -189,7 +189,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
Color color = EcsGUI.SelectPanelColor(meta, i, 9); Color color = EcsGUI.SelectPanelColor(meta, i, 9);
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(color, 0.2f))) using (EcsGUI.Layout.BeginVertical(color.SetAlpha(0.2f)))
{ {
GUILayout.Label(meta.TypeName); GUILayout.Label(meta.TypeName);
} }

View File

@ -91,7 +91,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
bool isNull = world == null || world.IsDestroyed || world.ID == 0; bool isNull = world == null || world.IsDestroyed || world.ID == 0;
if (isNull) { return; } if (isNull) { return; }
using (BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
IsShowRuntimeComponents = EditorGUILayout.BeginFoldoutHeaderGroup(IsShowRuntimeComponents, "RUNTIME COMPONENTS", EditorStyles.foldout); IsShowRuntimeComponents = EditorGUILayout.BeginFoldoutHeaderGroup(IsShowRuntimeComponents, "RUNTIME COMPONENTS", EditorStyles.foldout);
EditorGUILayout.EndFoldoutHeaderGroup(); EditorGUILayout.EndFoldoutHeaderGroup();
@ -127,7 +127,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
Color panelColor = SelectPanelColor(meta, index, total); Color panelColor = SelectPanelColor(meta, index, total);
GUILayout.BeginVertical(UnityEditorUtility.GetStyle(panelColor, EscEditorConsts.COMPONENT_DRAWER_ALPHA)); using (BeginVertical(panelColor.SetAlpha(EscEditorConsts.COMPONENT_DRAWER_ALPHA)))
{
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
////Close button ////Close button
@ -157,7 +158,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
cmp.SetRaw(worldID, resultData); cmp.SetRaw(worldID, resultData);
} }
GUILayout.EndVertical(); }
} }
} }
} }
@ -214,7 +215,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
private static List<IEcsPool> _componentPoolsBuffer; private static List<IEcsPool> _componentPoolsBuffer;
public static void DrawRuntimeComponents(int entityID, EcsWorld world, bool isWithFoldout = true) public static void DrawRuntimeComponents(int entityID, EcsWorld world, bool isWithFoldout = true)
{ {
using (BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
if (isWithFoldout) if (isWithFoldout)
{ {
@ -270,7 +271,9 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
Color panelColor = SelectPanelColor(meta, index, total); Color panelColor = SelectPanelColor(meta, index, total);
GUILayout.BeginVertical(UnityEditorUtility.GetStyle(panelColor, EscEditorConsts.COMPONENT_DRAWER_ALPHA));
using (BeginVertical(panelColor.SetAlpha(EscEditorConsts.COMPONENT_DRAWER_ALPHA)))
{
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
//Close button //Close button
@ -299,9 +302,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
pool.SetRaw(entityID, resultData); pool.SetRaw(entityID, resultData);
} }
}
GUILayout.EndVertical();
} }
} }

View File

@ -205,12 +205,26 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
public VerticalScope(GUILayoutOption[] options) { GUILayout.BeginVertical(options); } public VerticalScope(GUILayoutOption[] options) { GUILayout.BeginVertical(options); }
public VerticalScope(GUIStyle style, GUILayoutOption[] options) { GUILayout.BeginVertical(style, options); } public VerticalScope(GUIStyle style, GUILayoutOption[] options) { GUILayout.BeginVertical(style, options); }
public VerticalScope(Color backgroundColor, GUILayoutOption[] options)
{
using (SetColor(backgroundColor))
{
GUILayout.BeginVertical(UnityEditorUtility.GetWhiteStyle(), options);
}
}
public void Dispose() { GUILayout.EndVertical(); } public void Dispose() { GUILayout.EndVertical(); }
} }
public struct HorizontalScope : IDisposable public struct HorizontalScope : IDisposable
{ {
public HorizontalScope(GUILayoutOption[] options) { GUILayout.BeginHorizontal(options); } public HorizontalScope(GUILayoutOption[] options) { GUILayout.BeginHorizontal(options); }
public HorizontalScope(GUIStyle style, GUILayoutOption[] options) { GUILayout.BeginHorizontal(style, options); } public HorizontalScope(GUIStyle style, GUILayoutOption[] options) { GUILayout.BeginHorizontal(style, options); }
public HorizontalScope(Color backgroundColor, GUILayoutOption[] options)
{
using (SetColor(backgroundColor))
{
GUILayout.BeginHorizontal(UnityEditorUtility.GetWhiteStyle(), options);
}
}
public void Dispose() { GUILayout.EndHorizontal(); } public void Dispose() { GUILayout.EndHorizontal(); }
} }
public struct ScrollViewScope : IDisposable public struct ScrollViewScope : IDisposable
@ -221,14 +235,16 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
public static ScrollViewScope BeginScrollView(ref Vector2 pos) => new ScrollViewScope(ref pos, Array.Empty<GUILayoutOption>()); public static ScrollViewScope BeginScrollView(ref Vector2 pos) => new ScrollViewScope(ref pos, Array.Empty<GUILayoutOption>());
public static HorizontalScope BeginHorizontal() => new HorizontalScope(Array.Empty<GUILayoutOption>());
public static VerticalScope BeginVertical() => new VerticalScope(Array.Empty<GUILayoutOption>());
public static ScrollViewScope BeginScrollView(ref Vector2 pos, params GUILayoutOption[] options) => new ScrollViewScope(ref pos, options); public static ScrollViewScope BeginScrollView(ref Vector2 pos, params GUILayoutOption[] options) => new ScrollViewScope(ref pos, options);
public static HorizontalScope BeginHorizontal(params GUILayoutOption[] options) => new HorizontalScope(options);
public static VerticalScope BeginVertical(params GUILayoutOption[] options) => new VerticalScope(options);
public static ScrollViewScope BeginScrollView(ref Vector2 pos, GUIStyle style, params GUILayoutOption[] options) => new ScrollViewScope(ref pos, style, options); public static ScrollViewScope BeginScrollView(ref Vector2 pos, GUIStyle style, params GUILayoutOption[] options) => new ScrollViewScope(ref pos, style, options);
public static HorizontalScope BeginHorizontal() => new HorizontalScope(Array.Empty<GUILayoutOption>());
public static HorizontalScope BeginHorizontal(params GUILayoutOption[] options) => new HorizontalScope(options);
public static HorizontalScope BeginHorizontal(GUIStyle style, params GUILayoutOption[] options) => new HorizontalScope(style, options); public static HorizontalScope BeginHorizontal(GUIStyle style, params GUILayoutOption[] options) => new HorizontalScope(style, options);
public static HorizontalScope BeginHorizontal(Color backgroundColor, params GUILayoutOption[] options) => new HorizontalScope(backgroundColor, options);
public static VerticalScope BeginVertical() => new VerticalScope(Array.Empty<GUILayoutOption>());
public static VerticalScope BeginVertical(params GUILayoutOption[] options) => new VerticalScope(options);
public static VerticalScope BeginVertical(GUIStyle style, params GUILayoutOption[] options) => new VerticalScope(style, options); public static VerticalScope BeginVertical(GUIStyle style, params GUILayoutOption[] options) => new VerticalScope(style, options);
public static VerticalScope BeginVertical(Color backgroundColor, params GUILayoutOption[] options) => new VerticalScope(backgroundColor, options);
} }
public static CheckChangedScope CheckChanged() => CheckChangedScope.New(); public static CheckChangedScope CheckChanged() => CheckChangedScope.New();
public static CheckChangedScopeWithAutoApply CheckChanged(SerializedObject serializedObject) => new CheckChangedScopeWithAutoApply(serializedObject); public static CheckChangedScopeWithAutoApply CheckChanged(SerializedObject serializedObject) => new CheckChangedScopeWithAutoApply(serializedObject);
@ -1022,7 +1038,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
GetReferenceDropDown(sortedPredicateTypes, sortedWithOutTypes).Show(position); GetReferenceDropDown(sortedPredicateTypes, sortedWithOutTypes).Show(position);
} }
#endregion #endregion
} }
} }
#endif #endif

View File

@ -2,7 +2,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using UnityObject = UnityEngine.Object; using UnityObject = UnityEngine.Object;
@ -368,6 +367,36 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
#endregion #endregion
#region WhiteTexture
private static Texture2D _whiteTexture;
private static GUIStyle _whiteStyle;
private static GUIStyle _transperentBlackBackgrounStyle;
public static Texture2D GetWhiteTexture()
{
if (_whiteTexture == null)
{
_whiteTexture = CreateTexture(2, 2, Color.white);
}
return _whiteTexture;
}
public static GUIStyle GetWhiteStyle()
{
if (_whiteStyle == null || _whiteStyle.normal.background == null)
{
_whiteStyle = CreateStyle(GetWhiteTexture(), GUI.skin.label);
}
return _whiteStyle;
}
public static GUIStyle GetTransperentBlackBackgrounStyle()
{
if (_transperentBlackBackgrounStyle == null || _transperentBlackBackgrounStyle.normal.background == null)
{
_transperentBlackBackgrounStyle = CreateStyle(CreateTexture(2, 2, new Color(0, 0, 0, 0.2f)), GUI.skin.label);
}
return _transperentBlackBackgrounStyle;
}
#endregion
#region GetStyle #region GetStyle
public static GUIStyle GetStyle(Color color, float alphaMultiplier) public static GUIStyle GetStyle(Color color, float alphaMultiplier)
{ {
@ -376,26 +405,29 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
public static GUIStyle GetStyle(Color32 color32) public static GUIStyle GetStyle(Color32 color32)
{ {
int colorCode = new Color32Union(color32).colorCode; int colorCode = color32.GetCode32();
if (colorBoxeStyles.TryGetValue(colorCode, out GUIStyle style)) if (colorBoxeStyles.TryGetValue(colorCode, out GUIStyle style))
{ {
if (style == null || style.normal.background == null) if (style == null || style.normal.background == null)
{ {
style = CreateStyle(color32, colorCode); style = CreateStyle(CreateTexture(2, 2, color32));
colorBoxeStyles[colorCode] = style; colorBoxeStyles[colorCode] = style;
} }
return style; return style;
} }
style = CreateStyle(color32, colorCode); style = CreateStyle(CreateTexture(2, 2, color32));
colorBoxeStyles.Add(colorCode, style); colorBoxeStyles.Add(colorCode, style);
return style; return style;
} }
private static GUIStyle CreateStyle(Color32 color32, int colorCode) private static GUIStyle CreateStyle(Texture2D texture, GUIStyle referenceStyle = null)
{ {
if (referenceStyle == null)
{
referenceStyle = GUI.skin.box;
}
GUIStyle result = new GUIStyle(GUI.skin.box); GUIStyle result = new GUIStyle(GUI.skin.box);
Color componentColor = color32; Texture2D texture2D = texture;
Texture2D texture2D = CreateTexture(2, 2, componentColor);
result.hover.background = texture2D; result.hover.background = texture2D;
result.hover.scaledBackgrounds = Array.Empty<Texture2D>(); result.hover.scaledBackgrounds = Array.Empty<Texture2D>();
result.focused.background = texture2D; result.focused.background = texture2D;
@ -410,45 +442,15 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
var pixels = new Color[width * height]; var pixels = new Color[width * height];
for (var i = 0; i < pixels.Length; ++i) for (var i = 0; i < pixels.Length; ++i)
{
pixels[i] = color; pixels[i] = color;
}
var result = new Texture2D(width, height); var result = new Texture2D(width, height);
result.SetPixels(pixels); result.SetPixels(pixels);
result.Apply(); result.Apply();
return result; return result;
} }
#endregion #endregion
#region Utils
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)]
private readonly ref struct Color32Union
{
[FieldOffset(0)]
public readonly int colorCode;
[FieldOffset(0)]
public readonly byte r;
[FieldOffset(1)]
public readonly byte g;
[FieldOffset(2)]
public readonly byte b;
[FieldOffset(3)]
public readonly byte a;
public Color32Union(byte r, byte g, byte b, byte a) : this()
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public Color32Union(Color32 color) : this()
{
r = color.r;
g = color.g;
b = color.b;
a = color.a;
}
}
#endregion
} }
internal static class RuntimeComponentsUtility internal static class RuntimeComponentsUtility

View File

@ -21,5 +21,10 @@ namespace DCFApixels.DragonECS.Unity.Internal
self.a = a; self.a = a;
return self; return self;
} }
public unsafe static int GetCode32(this Color32 self)
{
return *(int*)&self;
}
} }
} }

View File

@ -178,7 +178,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
private void DrawRecordList(SerializedProperty recordsProp) private void DrawRecordList(SerializedProperty recordsProp)
{ {
GUILayout.Label(UnityEditorUtility.GetLabel(recordsProp.displayName), EditorStyles.boldLabel); GUILayout.Label(UnityEditorUtility.GetLabel(recordsProp.displayName), EditorStyles.boldLabel);
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
GUILayout.Space(4f); GUILayout.Space(4f);

View File

@ -1,9 +1,7 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using DCFApixels.DragonECS.Unity.Internal; using DCFApixels.DragonECS.Unity.Internal;
using DCFApixels.DragonECS.Unity.RefRepairer.Editors; using DCFApixels.DragonECS.Unity.RefRepairer.Editors;
using System;
using UnityEditor; using UnityEditor;
using UnityEditor.Graphs;
using UnityEditorInternal; using UnityEditorInternal;
using UnityEngine; using UnityEngine;
@ -179,7 +177,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
} }
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(Color.black, 0.2f))) using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetTransperentBlackBackgrounStyle()))
{ {
DrawTop(Target, _componentsProp); DrawTop(Target, _componentsProp);
_reorderableComponentsList.DoLayoutList(); _reorderableComponentsList.DoLayoutList();

View File

@ -94,7 +94,7 @@ namespace DCFApixels.DragonECS.Unity.Docs.Editors
GUILayout.Space(EditorGUIUtility.standardVerticalSpacing * -2f); GUILayout.Space(EditorGUIUtility.standardVerticalSpacing * -2f);
DataScrolPosition = GUILayout.BeginScrollView(DataScrolPosition, UnityEditorUtility.GetStyle(Color.black, 0.2f), GUILayout.ExpandWidth(true)); DataScrolPosition = GUILayout.BeginScrollView(DataScrolPosition, UnityEditorUtility.GetTransperentBlackBackgrounStyle(), GUILayout.ExpandWidth(true));
DrawSelectedGroupMeta(selectedGroupInfo); DrawSelectedGroupMeta(selectedGroupInfo);
GUILayout.EndScrollView(); GUILayout.EndScrollView();
@ -251,7 +251,7 @@ namespace DCFApixels.DragonECS.Unity.Docs.Editors
Color alphaPanelColor = panelColor; Color alphaPanelColor = panelColor;
alphaPanelColor.a = EscEditorConsts.COMPONENT_DRAWER_ALPHA; alphaPanelColor.a = EscEditorConsts.COMPONENT_DRAWER_ALPHA;
using (EcsGUI.Layout.BeginVertical(UnityEditorUtility.GetStyle(alphaPanelColor))) using (EcsGUI.Layout.BeginVertical(alphaPanelColor))
{ {
GUILayout.Space(1f); GUILayout.Space(1f);