Init commit/Add systems debug/Add new runners

This commit is contained in:
Mikhail 2023-03-26 11:20:02 +08:00
parent 898028ff98
commit 9509da8b95
19 changed files with 413 additions and 0 deletions

16
DragonECS-Unity.asmdef Normal file
View File

@ -0,0 +1,16 @@
{
"name": "DCFApixels.DragonECS-Unity",
"rootNamespace": "DCFApixels",
"references": [
"GUID:abb125fa67fff1e45914d0825236f608"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: e1b0feb9d48fee64da21a08eeb2fafd4
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
LICENSE.meta Normal file
View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 0ef75f5af61ea7c4ea8fe3ef92d02b35
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
README.md.meta Normal file
View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1f63a4d70d00eb641ba88fa50962d963
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
src.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6d12f3413368d5c4aa87dcfe7c4c2792
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

106
src/GameObjectRef.cs Normal file
View File

@ -0,0 +1,106 @@
using System.Runtime.CompilerServices;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace DCFApixels.DragonECS.Unity
{
public struct GameObjectRef
{
public GameObject gameObject;
public Transform transform;
public string Name
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => gameObject.name;
}
public GameObjectRef(GameObject gameObject)
{
this.gameObject = gameObject;
transform = gameObject.transform;
}
}
public enum GameObjectIcon : byte
{
NONE,
Label_Gray,
Label_Blue,
Label_Teal,
Label_Green,
Label_Yellow,
Label_Orange,
Label_Red,
Label_Purple,
Circle_Gray,
Circle_Blue,
Circle_Teal,
Circle_Green,
Circle_Yellow,
Circle_Orange,
Circle_Red,
Circle_Purple,
Diamond_Gray,
Diamond_Blue,
Diamond_Teal,
Diamond_Green,
Diamond_Yellow,
Diamond_Orange,
Diamond_Red,
Diamond_Purple
}
public static class GameObjectIconConsts
{
public const int RAW_LABEL_ICON_LAST = (int)GameObjectIcon.Label_Purple;
}
public static class GameObjectRefExt
{
public static ent NewEntityWithGameObject(this IEcsWorld self, string name = "Entity", GameObjectIcon icon = GameObjectIcon.NONE)
{
ent result = self.NewEntity();
GameObject newGameObject = new GameObject(name);
newGameObject.AddComponent<EcsEntity>()._entity = result;
result.Write<GameObjectRef>() = new GameObjectRef(newGameObject);
#if UNITY_EDITOR
if (icon != GameObjectIcon.NONE)
{
string contentName;
int number = (int)icon - 1;
if (number < GameObjectIconConsts.RAW_LABEL_ICON_LAST)
{
contentName = $"sv_label_{number}";
}
else
{
number -= GameObjectIconConsts.RAW_LABEL_ICON_LAST;
contentName = $"sv_icon_dot{number}_pix16_gizmo";
}
GUIContent iconContent = EditorGUIUtility.IconContent(contentName);
EditorGUIUtility.SetIconForObject(newGameObject, (Texture2D)iconContent.image);
}
#endif
return result;
}
}
public class EcsEntity : MonoBehaviour
{
internal ent _entity;
public ent entity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity;
}
public bool IsAlive
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity.IsAlive();
}
}
}

11
src/GameObjectRef.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 909b2b01fa1e58b4e9e739827e36cff4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
src/Runner.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 70c8e3cb9125ee14fad9fdee48c3e8ba
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
namespace DCFApixels.DragonECS.Unity
{
public interface IEcsFixedRunSystem : IEcsSystem
{
public void FixedRun(EcsSystems systems);
}
public class EcsFixedRunSystemRunner : EcsRunner<IEcsFixedRunSystem>, IEcsFixedRunSystem
{
void IEcsFixedRunSystem.FixedRun(EcsSystems systems)
{
foreach (var item in targets) item.FixedRun(systems);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aff69967c9d73594283178ea5530cf71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
namespace DCFApixels.DragonECS.Unity
{
public interface IEcsLateRunSystem : IEcsSystem
{
public void LateRun(EcsSystems systems);
}
public class EcsLateRunSystemRunner : EcsRunner<IEcsLateRunSystem>, IEcsLateRunSystem
{
void IEcsLateRunSystem.LateRun(EcsSystems systems)
{
foreach (var item in targets) item.LateRun(systems);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 385a6c66660032944ad2cce7130715d7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

127
src/SystemsDebugSystem.cs Normal file
View File

@ -0,0 +1,127 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DCFApixels.DragonECS.Unity
{
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;
internal bool showInterfaces = false;
}
#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;
if(_headerStyle == null)
{
_headerStyle = new GUIStyle(EditorStyles.boldLabel);
_interfacesStyle = new GUIStyle(EditorStyles.helpBox);
_interfacesStyle.hover.textColor = _interfaceColor;
_interfacesStyle.focused.textColor = _interfaceColor;
_interfacesStyle.active.textColor = _interfaceColor;
_interfacesStyle.normal.textColor = _interfaceColor;
_headerStyle.fontSize = 28;
}
GUILayout.Label("[Systems]", _headerStyle);
Target.showInterfaces = EditorGUILayout.Toggle("Show Interfaces", Target.showInterfaces);
foreach (var item in Target.systems.AllSystems)
{
DrawSystem(item);
}
GUILayout.Label("[Runners]", _headerStyle);
foreach (var item in Target.systems.AllRunners)
{
DrawRunner(item.Value);
}
}
private void DrawSystem(IEcsSystem system)
{
Type type = system.GetType();
string name = type.Name;
Color color = (GetAttribute<DebugColorAttribute>(type) ?? _fakeDebugColorAttribute).GetUnityColor();
Color defaultBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = color;
GUILayout.BeginVertical(EditorStyles.helpBox);
if (Target.showInterfaces)
{
GUILayout.Label(string.Join(", ", type.GetInterfaces().Select(o => o.Name)), _interfacesStyle);
}
GUILayout.Label(name, EditorStyles.boldLabel);
GUILayout.EndVertical();
GUI.backgroundColor = defaultBackgroundColor;
}
private void DrawRunner(IEcsRunner runner)
{
Type type = runner.GetType();
Color color = (GetAttribute<DebugColorAttribute>(type) ?? _fakeDebugColorAttribute).GetUnityColor();
Color defaultBackgroundColor = GUI.backgroundColor;
GUI.backgroundColor = 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();
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;
}
}
}
#endif
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ed7ff62966b45141b388716e91dfac0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
src/Utils.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6c2eeaf69783f0b41a968e7e4d539cf3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,12 @@
using UnityEngine;
namespace DCFApixels.DragonECS.Unity
{
public static class DebugColorAttributeExt
{
public static Color GetUnityColor(this DebugColorAttribute self)
{
return new Color(self.r / 255f, self.g / 255f, self.b / 255f);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1fae683793d3ed345ad3bf19731db13e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
src/WorldDebugSystem.cs Normal file
View File

@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DCFApixels.DragonECS.Unity
{
public class WorldDebugSystem : MonoBehaviour
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1d334d67cff28704c94924d2e6c8df84
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: