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

162 lines
6.1 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2023-03-29 15:48:18 +08:00
using System.Reflection;
namespace DCFApixels.DragonECS.Unity
{
2023-03-29 15:48:18 +08:00
[DebugHide, DebugColor(DebugColor.Gray)]
public class SystemsDebugSystem : IEcsPreInitSystem
{
private string _name;
public SystemsDebugSystem(string name = "Systems")
{
_name = name;
}
void IEcsPreInitSystem.PreInit(EcsSystems systems)
{
SystemsDebugMonitor monitor = new GameObject(EcsConsts.DEBUG_PREFIX + _name).AddComponent<SystemsDebugMonitor>();
monitor.source = this;
monitor.systems = systems;
monitor.systemsName = _name;
Object.DontDestroyOnLoad(monitor.gameObject);
}
}
public class SystemsDebugMonitor : MonoBehaviour
{
internal SystemsDebugSystem source;
internal EcsSystems systems;
internal string systemsName;
}
#if UNITY_EDITOR
namespace Editors
{
using System;
using System.Linq;
using UnityEditor;
[CustomEditor(typeof(SystemsDebugMonitor))]
public class SystemsDebugMonitorEditor : Editor
{
private DebugColorAttribute _fakeDebugColorAttribute = new DebugColorAttribute(DebugColor.White);
private Type _debugColorAttributeType = typeof(DebugColorAttribute);
private GUIStyle _headerStyle;
private GUIStyle _interfacesStyle;
private Color _interfaceColor = new Color(0.96f, 1f, 0.16f);
private SystemsDebugMonitor Target => (SystemsDebugMonitor)target;
public override void OnInspectorGUI()
{
if (Target.source == null)
return;
2023-03-27 20:49:27 +08:00
if (_headerStyle == null)
{
_headerStyle = new GUIStyle(EditorStyles.boldLabel);
2023-03-29 15:48:18 +08:00
_interfacesStyle = new GUIStyle(EditorStyles.miniLabel);
_interfacesStyle.hover.textColor = _interfaceColor;
_interfacesStyle.focused.textColor = _interfaceColor;
_interfacesStyle.active.textColor = _interfaceColor;
_interfacesStyle.normal.textColor = _interfaceColor;
_headerStyle.fontSize = 28;
}
GUILayout.Label("[Systems]", _headerStyle);
2023-03-29 15:48:18 +08:00
DebugMonitorPrefs.instance.isShowInterfaces = EditorGUILayout.Toggle("Show Interfaces", DebugMonitorPrefs.instance.isShowInterfaces);
DebugMonitorPrefs.instance.isShowHidden = EditorGUILayout.Toggle("Show Hidden", DebugMonitorPrefs.instance.isShowHidden);
2023-03-27 20:49:27 +08:00
GUILayout.BeginVertical();
foreach (var item in Target.systems.AllSystems)
{
DrawSystem(item);
}
2023-03-27 20:49:27 +08:00
GUILayout.EndVertical();
2023-03-29 15:48:18 +08:00
GUILayout.Label("[Runners]", _headerStyle);
2023-03-29 15:48:18 +08:00
GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black));
foreach (var item in Target.systems.AllRunners)
{
DrawRunner(item.Value);
}
2023-03-29 15:48:18 +08:00
GUILayout.EndVertical();
}
private void DrawSystem(IEcsSystem system)
{
2023-03-27 20:49:27 +08:00
if(system is SystemsBlockMarkerSystem markerSystem)
{
GUILayout.EndVertical();
GUILayout.BeginVertical(EcsEditor.GetStyle(Color.black));
GUILayout.BeginHorizontal();
GUILayout.Label("<");
GUILayout.Label($"{markerSystem.name}", EditorStyles.boldLabel);
GUILayout.Label(">", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
return;
}
Type type = system.GetType();
2023-03-29 15:48:18 +08:00
if (CheckIsHidden(type))
return;
string name = type.Name;
Color color = (GetAttribute<DebugColorAttribute>(type) ?? _fakeDebugColorAttribute).GetUnityColor();
2023-03-27 20:31:58 +08:00
//Color defaultBackgroundColor = GUI.backgroundColor;
//GUI.backgroundColor = color;
GUILayout.BeginVertical(EcsEditor.GetStyle(color));
2023-03-29 15:48:18 +08:00
if (DebugMonitorPrefs.instance.isShowInterfaces)
{
GUILayout.Label(string.Join(", ", type.GetInterfaces().Select(o => o.Name)), _interfacesStyle);
}
GUILayout.Label(name, EditorStyles.boldLabel);
GUILayout.EndVertical();
2023-03-27 20:31:58 +08:00
//GUI.backgroundColor = defaultBackgroundColor;
}
private void DrawRunner(IEcsRunner runner)
{
Type type = runner.GetType();
2023-03-29 15:48:18 +08:00
if (CheckIsHidden(type))
return;
Color color = (GetAttribute<DebugColorAttribute>(type) ?? _fakeDebugColorAttribute).GetUnityColor();
2023-03-27 20:31:58 +08:00
//Color defaultBackgroundColor = GUI.backgroundColor;
//GUI.backgroundColor = color;
GUILayout.BeginVertical(EcsEditor.GetStyle(color));
//GUILayout.BeginVertical(EditorStyles.helpBox);
GUILayout.Label(type.Name, EditorStyles.boldLabel);
GUILayout.Label(string.Join(", ", runner.Targets.Cast<object>().Select(o => o.GetType().Name)));
GUILayout.EndVertical();
2023-03-27 20:31:58 +08:00
//GUI.backgroundColor = defaultBackgroundColor;
}
private TAttribute GetAttribute<TAttribute>(Type target) where TAttribute : Attribute
{
var result = target.GetCustomAttributes(_debugColorAttributeType, false);
if (result.Length > 0)
return (TAttribute)result[0];
return null;
}
2023-03-29 15:48:18 +08:00
private bool CheckIsHidden(Type target)
{
if (DebugMonitorPrefs.instance.isShowHidden)
return false;
return target.GetCustomAttribute<DebugHideAttribute>() != null;
}
}
}
#endif
}