DragonECS-Unity/src/Debug/Systems/WorldDebugSystem.cs

181 lines
6.2 KiB
C#
Raw Normal View History

using DCFApixels.DragonECS.Unity.Debug;
2023-03-29 15:48:18 +08:00
using UnityEngine;
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS
2023-03-29 15:48:18 +08:00
{
2023-04-26 16:54:27 +08:00
[DebugHide, DebugColor(DebugColor.Gray)]
2023-06-22 14:40:26 +08:00
public class WorldDebugSystem : IEcsRunProcess
2023-03-29 15:48:18 +08:00
{
private string _monitorName;
2023-04-24 16:48:26 +08:00
private EcsWorld _ecsWorld;
2023-03-29 15:48:18 +08:00
2023-05-07 00:50:44 +08:00
public WorldDebugSystem(EcsWorld ecsWorld, string monitorName = null)
2023-03-29 15:48:18 +08:00
{
_monitorName = monitorName;
2023-05-07 00:50:44 +08:00
if (string.IsNullOrEmpty(_monitorName)) _monitorName = ecsWorld.GetType().Name;
2023-03-29 15:48:18 +08:00
_ecsWorld = ecsWorld;
WorldDebugMonitor monitor = new GameObject(EcsConsts.DEBUG_PREFIX + _monitorName).AddComponent<WorldDebugMonitor>();
2023-05-07 00:50:44 +08:00
WorldPoolsMonitor poolsmonitor = new GameObject(EcsConsts.DEBUG_PREFIX + "Pools").AddComponent<WorldPoolsMonitor>();
poolsmonitor.transform.SetParent(monitor.transform);
monitor.source = this;
monitor.world = _ecsWorld;
monitor.monitorName = _monitorName;
poolsmonitor.source = this;
poolsmonitor.world = _ecsWorld;
2023-04-24 16:48:26 +08:00
poolsmonitor.monitorName = "pools";
2023-03-29 15:48:18 +08:00
}
2023-03-30 05:33:35 +08:00
public void Run(EcsPipeline pipeline)
2023-03-29 15:48:18 +08:00
{
}
}
public class WorldDebugMonitor : DebugMonitorBase
{
internal WorldDebugSystem source;
2023-04-24 16:48:26 +08:00
internal EcsWorld world;
}
#if UNITY_EDITOR
namespace Editors
{
using UnityEditor;
[CustomEditor(typeof(WorldDebugMonitor))]
public class WorldDebugMonitorEditor : Editor
{
private WorldDebugMonitor Target => (WorldDebugMonitor)target;
2023-05-07 00:50:44 +08:00
public override void OnInspectorGUI()
{
GUILayout.Label($"Size: {Target.world.Capacity}");
GUILayout.Label($"Total entities: {Target.world.Count}");
}
}
}
#endif
public class WorldPoolsMonitor : DebugMonitorBase
{
internal WorldDebugSystem source;
2023-04-24 16:48:26 +08:00
internal EcsWorld world;
}
#if UNITY_EDITOR
namespace Editors
{
using System.Linq;
using System.Reflection;
2023-06-22 14:40:26 +08:00
using UnityEditor;
[CustomEditor(typeof(WorldPoolsMonitor))]
public class WorldPoolsMonitorEditor : Editor
{
private static Vector2 _poolBlockMinSize = new Vector2(80, 160);
private static Vector2 _poolProgressBasrSize = _poolBlockMinSize * new Vector2(1f, 0.8f);
private WorldPoolsMonitor Target => (WorldPoolsMonitor)target;
private Vector2 _scroll;
public override void OnInspectorGUI()
{
2023-06-22 14:40:26 +08:00
_scroll = GUILayout.BeginScrollView(_scroll, GUILayout.Height(800f));
var pools = Target.world.AllPools.ToArray().Where(o => !o.IsNullOrDummy()).OfType<IEcsPool>();
GUILayout.Label("", GUILayout.ExpandWidth(true));
float width = GUILayoutUtility.GetLastRect().width;
Vector3 newPoolBlockSize = _poolBlockMinSize;
int widthCount = Mathf.Max(1, Mathf.Min((Mathf.FloorToInt(width / _poolBlockMinSize.x)), pools.Count()));
newPoolBlockSize.x = width / widthCount;
int x = -1, y = 0;
foreach (var pool in pools)
{
if (++x >= widthCount)
{
x = 0;
y++;
}
DrawPoolBlock(pool, new Rect(newPoolBlockSize.x * x, newPoolBlockSize.y * y, newPoolBlockSize.x, newPoolBlockSize.y));
}
GUILayout.EndScrollView();
}
2023-05-07 00:50:44 +08:00
private void DrawPoolBlock(IEcsPool pool, Rect position)
{
int count = pool.Count;
int capacity = pool.Capacity < 0 ? count : pool.Capacity;
Color defaultContentColor = GUI.contentColor;
GUI.contentColor = Color.black * 0.925f;
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
position = AddMargin(position, 1f, 1f);
2023-06-22 14:40:26 +08:00
EditorGUI.DrawRect(position, Color.black * 0.16f);
2023-05-07 00:50:44 +08:00
Rect progressBar = new Rect(Vector2.zero, _poolProgressBasrSize);
progressBar.width = position.width;
progressBar.center = position.center - Vector2.up * _poolBlockMinSize.y * 0.09f;
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
Color mainColor = new Color(0.3f, 1f, 0f, 1f);
var debugColor = pool.ComponentType.GetCustomAttribute<DebugColorAttribute>();
if (debugColor != null)
{
mainColor = debugColor.GetUnityColor();
}
Color backgroundColor = mainColor * 0.3f + Color.white * 0.2f;
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
EditorGUI.DrawRect(progressBar, backgroundColor);
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
progressBar.yMin = progressBar.yMax - ((float)count / capacity) * progressBar.height;
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
GUIStyle textStyle0 = new GUIStyle(EditorStyles.miniBoldLabel);
textStyle0.alignment = TextAnchor.MiddleCenter;
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
Color foregroundColor = mainColor;
EditorGUI.DrawRect(progressBar, foregroundColor);
GUI.Label(progressBar, count.ToString(), textStyle0);
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
GUIStyle textStyle1 = new GUIStyle(EditorStyles.miniBoldLabel);
textStyle1.alignment = TextAnchor.UpperCenter;
2023-06-22 14:40:26 +08:00
GUI.Label(AddMargin(position, 3f, 3f), "Total\r\n" + capacity, textStyle1);
2023-05-07 00:50:44 +08:00
GUI.contentColor = defaultContentColor;
GUIStyle textStyle2 = new GUIStyle(EditorStyles.miniBoldLabel);
textStyle2.wordWrap = true;
textStyle2.alignment = TextAnchor.LowerCenter;
string name = EcsEditor.GetGenericName(pool.ComponentType);
2023-05-23 01:48:54 +08:00
GUIContent label = new GUIContent(name, $"{name} e:{count}");
2023-05-07 00:50:44 +08:00
GUI.Label(AddMargin(position, -10f, 3f), label, textStyle2);
2023-06-22 14:40:26 +08:00
2023-05-07 00:50:44 +08:00
}
private Rect AddMargin(Rect rect, Vector2 value)
{
return AddMargin(rect, value.x, value.y);
}
private Rect AddMargin(Rect rect, float x, float y)
{
rect.yMax -= y;
rect.yMin += y;
rect.xMax -= x;
rect.xMin += x;
return rect;
}
}
}
#endif
2023-03-29 15:48:18 +08:00
}