Skip to content

Commit

Permalink
* EcsWorldUnityEditorSystem: renamed to EcsWorldDebugSystem.
Browse files Browse the repository at this point in the history
* Mass refactoring.
  • Loading branch information
Leopotam committed Jun 20, 2021
1 parent 7d71c4c commit c669917
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 288 deletions.
37 changes: 14 additions & 23 deletions Editor/EcsEntity.cs → Editor/EcsEntityDebugView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,38 @@
using UnityEngine;

namespace Leopotam.EcsLite.UnityEditor {
[CustomEditor (typeof (EcsEntityObserver))]
sealed class EcsEntityObserverInspector : Editor {
[CustomEditor (typeof (EcsEntityDebugView))]
sealed class EcsEntityDebugViewInspector : Editor {
const int MaxFieldToStringLength = 128;

static object[] _componentsCache = new object[32];

EcsEntityObserver _observer;

public override void OnInspectorGUI () {
if (_observer.World != null) {
var observer = (EcsEntityDebugView) target;
if (observer.World != null) {
var guiEnabled = GUI.enabled;
GUI.enabled = true;
DrawComponents ();
DrawComponents (observer);
GUI.enabled = guiEnabled;
EditorUtility.SetDirty (target);
}
}

void OnEnable () {
_observer = target as EcsEntityObserver;
}

void OnDisable () {
_observer = null;
}

void DrawComponents () {
if (_observer.gameObject.activeSelf) {
var count = _observer.World.GetComponents (_observer.Entity, ref _componentsCache);
void DrawComponents (EcsEntityDebugView debugView) {
if (debugView.gameObject.activeSelf) {
var count = debugView.World.GetComponents (debugView.Entity, ref _componentsCache);
for (var i = 0; i < count; i++) {
var component = _componentsCache[i];
_componentsCache[i] = null;
var type = component.GetType ();
GUILayout.BeginVertical (GUI.skin.box);
var typeName = EditorHelpers.GetCleanGenericTypeName (type);
if (!EcsComponentInspectors.Render (typeName, type, component, _observer)) {
var typeName = EditorExtensions.GetCleanGenericTypeName (type);
if (!EcsComponentInspectors.Render (typeName, type, component, debugView)) {
EditorGUILayout.LabelField (typeName, EditorStyles.boldLabel);
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel++;
foreach (var field in type.GetFields (BindingFlags.Instance | BindingFlags.Public)) {
DrawTypeField (component, field, _observer);
DrawTypeField (component, field, debugView);
}
EditorGUI.indentLevel = indent;
}
Expand All @@ -62,7 +53,7 @@ void DrawComponents () {
}
}

void DrawTypeField (object instance, FieldInfo field, EcsEntityObserver entity) {
void DrawTypeField (object instance, FieldInfo field, EcsEntityDebugView entity) {
var fieldValue = field.GetValue (instance);
var fieldType = field.FieldType;
if (!EcsComponentInspectors.Render (field.Name, fieldType, fieldValue, entity)) {
Expand Down Expand Up @@ -107,9 +98,9 @@ static EcsComponentInspectors () {
}
}

public static bool Render (string label, Type type, object value, EcsEntityObserver observer) {
public static bool Render (string label, Type type, object value, EcsEntityDebugView debugView) {
if (Inspectors.TryGetValue (type, out var inspector)) {
inspector.OnGUI (label, value, observer.World, observer.Entity);
inspector.OnGUI (label, value, debugView.World, debugView.Entity);
return true;
}
return false;
Expand Down
File renamed without changes.
28 changes: 0 additions & 28 deletions Editor/EcsWorld.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Editor/EcsWorld.cs.meta

This file was deleted.

22 changes: 10 additions & 12 deletions Editor/Templates/Startup.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ using UnityEngine;

namespace #NS# {
sealed class #SCRIPTNAME# : MonoBehaviour {
EcsWorld _world;
EcsSystems _systems;

void Start () {
_world = new EcsWorld ();
// register your shared data here, for example:
// var shared = new Shared ();
// systems = new EcsSystems (world, shared);
_systems = new EcsSystems (_world);
#if UNITY_EDITOR
Leopotam.EcsLite.UnityEditor.EcsWorldObserver.Create (_world);
#endif
// systems = new EcsSystems (new EcsWorld (), shared);
_systems = new EcsSystems (new EcsWorld ());
_systems
// register your systems here, for example:
// .Add (new TestSystem1 ())
Expand All @@ -27,7 +22,11 @@ namespace #NS# {
// for components cleanup (order is important), for example:
// .DelHere<TestComponent1> ()
// .DelHere<TestComponent2> ()

#if UNITY_EDITOR
// add debug systems for custom worlds here, for example:
// .Add (new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem ("events"))
.Add (new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem ())
#endif
.Init ();
}

Expand All @@ -38,12 +37,11 @@ namespace #NS# {
void OnDestroy () {
if (_systems != null) {
_systems.Destroy ();
// add here cleanup for custom worlds, for example:
// _systems.GetWorld ("events").Destroy ();
_systems.GetWorld ().Destroy ();
_systems = null;
}
if (_world != null) {
_world.Destroy ();
_world = null;
}
}
}
}
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ If you can't / don't want to use unity modules, code can be cloned or downloaded
```csharp
// ecs-startup code:
void Start () {
_world = new EcsWorld ();
_systems = new EcsSystems (_world);
#if UNITY_EDITOR
Leopotam.EcsLite.UnityEditor.EcsWorldObserver.Create (_world);
#endif
_systems = new EcsSystems (new EcsWorld ());
_systems
.Add (new TestSystem1 ())
#if UNITY_EDITOR
// add debug systems for custom worlds here, for example:
// .Add (new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem ("events"))
.Add (new Leopotam.EcsLite.UnityEditor.EcsWorldDebugSystem ())
#endif
.Init ();
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
using Object = UnityEngine.Object;

namespace Leopotam.EcsLite.UnityEditor {
public sealed class EcsWorldUnityEditorSystem : IEcsPreInitSystem, IEcsRunSystem, IEcsWorldEventListener {
public sealed class EcsWorldDebugSystem : IEcsPreInitSystem, IEcsRunSystem, IEcsWorldEventListener {
readonly string _worldName;
readonly GameObject _rootGO;
readonly Transform _entitiesRoot;
readonly bool _bakeComponentsInName;
EcsWorld _world;
EcsEntityObserver[] _entities;
EcsEntityDebugView[] _entities;
Dictionary<int, byte> _dirtyEntities;
Type[] _typesCache;

public EcsWorldUnityEditorSystem (string worldName = null, bool bakeComponentsInName = true) {
public EcsWorldDebugSystem (string worldName = null, bool bakeComponentsInName = true) {
_bakeComponentsInName = bakeComponentsInName;
_worldName = worldName;
_rootGO = new GameObject (_worldName != null ? $"[ECS-WORLD {_worldName}]" : "[ECS-WORLD]");
Expand All @@ -35,7 +35,7 @@ public EcsWorldUnityEditorSystem (string worldName = null, bool bakeComponentsIn
public void PreInit (EcsSystems systems) {
_world = systems.GetWorld (_worldName);
if (_world == null) { throw new Exception ("Cant find required world."); }
_entities = new EcsEntityObserver [_world.GetWorldSize ()];
_entities = new EcsEntityDebugView [_world.GetWorldSize ()];
_dirtyEntities = new Dictionary<int, byte> (_entities.Length);
_world.AddEventListener (this);
}
Expand All @@ -47,7 +47,7 @@ public void Run (EcsSystems systems) {
if (_world.GetEntityGen (entity) > 0) {
var count = _world.GetComponentTypes (entity, ref _typesCache);
for (var i = 0; i < count; i++) {
entityName = $"{entityName}:{EditorHelpers.GetCleanGenericTypeName (_typesCache[i])}";
entityName = $"{entityName}:{EditorExtensions.GetCleanGenericTypeName (_typesCache[i])}";
}
}
_entities[entity].name = entityName;
Expand All @@ -59,7 +59,7 @@ public void OnEntityCreated (int entity) {
if (!_entities[entity]) {
var go = new GameObject ();
go.transform.SetParent (_entitiesRoot, false);
var entityObserver = go.AddComponent<EcsEntityObserver> ();
var entityObserver = go.AddComponent<EcsEntityDebugView> ();
entityObserver.Entity = entity;
entityObserver.World = _world;
_entities[entity] = entityObserver;
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions Runtime/EditorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// ----------------------------------------------------------------------------
// The MIT License
// UnityEditor integration https://github.com/Leopotam/ecslite-unityeditor
// for LeoECS Lite https://github.com/Leopotam/ecslite
// Copyright (c) 2021 Leopotam <[email protected]>
// ----------------------------------------------------------------------------

#if UNITY_EDITOR
using System;
using UnityEngine;

namespace Leopotam.EcsLite.UnityEditor {
public static class EditorExtensions {
public static string GetCleanGenericTypeName (Type type) {
if (!type.IsGenericType) {
return type.Name;
}
var constraints = "";
foreach (var constraint in type.GetGenericArguments ()) {
constraints += constraints.Length > 0 ? $", {GetCleanGenericTypeName (constraint)}" : constraint.Name;
}
return $"{type.Name.Substring (0, type.Name.LastIndexOf ("`", StringComparison.Ordinal))}<{constraints}>";
}
}

public sealed class EcsEntityDebugView : MonoBehaviour {
[NonSerialized]
public EcsWorld World;
[NonSerialized]
public int Entity;
}
}
#endif
3 changes: 3 additions & 0 deletions Runtime/EditorExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c669917

Please sign in to comment.