From 4d2d0cbb7701c7e34bbe9af8ffaa11ab7e5ac943 Mon Sep 17 00:00:00 2001 From: RealityStop Date: Sun, 20 Jun 2021 19:47:04 -0400 Subject: [PATCH] Added manual naming to the new ecslite-unityeditor --- Runtime/EcsWorldDebugSystem.cs | 42 ++++++++++++++++++++------ Runtime/UnityDebugName.cs | 55 ++++++++++++++++++++++++++++++++++ Runtime/UnityDebugName.cs.meta | 3 ++ 3 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 Runtime/UnityDebugName.cs create mode 100644 Runtime/UnityDebugName.cs.meta diff --git a/Runtime/EcsWorldDebugSystem.cs b/Runtime/EcsWorldDebugSystem.cs index af96c7b..125bfbb 100644 --- a/Runtime/EcsWorldDebugSystem.cs +++ b/Runtime/EcsWorldDebugSystem.cs @@ -20,6 +20,8 @@ public sealed class EcsWorldDebugSystem : IEcsPreInitSystem, IEcsRunSystem, IEcs EcsEntityDebugView[] _entities; Dictionary _dirtyEntities; Type[] _typesCache; + private EcsPool _namePool; + private EcsFilter _debugNameFilter; public EcsWorldDebugSystem (string worldName = null, bool bakeComponentsInName = true) { _bakeComponentsInName = bakeComponentsInName; @@ -38,21 +40,43 @@ public void PreInit (EcsSystems systems) { _entities = new EcsEntityDebugView [_world.GetWorldSize ()]; _dirtyEntities = new Dictionary (_entities.Length); _world.AddEventListener (this); + if (!_bakeComponentsInName) + { + _namePool = _world.GetPool(); + _debugNameFilter = _world.Filter().End(); + } } public void Run (EcsSystems systems) { - foreach (var pair in _dirtyEntities) { - var entity = pair.Key; - var entityName = entity.ToString ("X8"); - if (_world.GetEntityGen (entity) > 0) { - var count = _world.GetComponentTypes (entity, ref _typesCache); - for (var i = 0; i < count; i++) { - entityName = $"{entityName}:{EditorExtensions.GetCleanGenericTypeName (_typesCache[i])}"; + if (_bakeComponentsInName) + { + foreach (var pair in _dirtyEntities) + { + var entity = pair.Key; + var entityName = entity.ToString("X8"); + if (_world.GetEntityGen(entity) > 0) + { + var count = _world.GetComponentTypes(entity, ref _typesCache); + for (var i = 0; i < count; i++) + { + entityName = $"{entityName}:{EditorExtensions.GetCleanGenericTypeName(_typesCache[i])}"; + } } + + _entities[entity].name = entityName; + } + + _dirtyEntities.Clear(); + } + else + { + foreach (var entity in _debugNameFilter) + { + ref UnityDebugName nameComponent = ref _namePool.Get(entity); + if (nameComponent.changed) + _entities[entity].name = nameComponent.DisplayName; } - _entities[entity].name = entityName; } - _dirtyEntities.Clear (); } public void OnEntityCreated (int entity) { diff --git a/Runtime/UnityDebugName.cs b/Runtime/UnityDebugName.cs new file mode 100644 index 0000000..73b68e4 --- /dev/null +++ b/Runtime/UnityDebugName.cs @@ -0,0 +1,55 @@ +using Leopotam.EcsLite.UnityEditor; +using UnityEngine; + +namespace Leopotam.EcsLite +{ + public struct UnityDebugName + { + public string DisplayName; + + public bool changed; + } + + public static class UnityDebugNamingExt + { + /// + /// Creates a new entity. If running inside the unity editor, the passed name will be used + /// in the debug hierarchy. (In builds, this is ignored) + /// + public static int NewEntity(this EcsWorld world, string entityName) + { + int result = world.NewEntity(); +#if UNITY_EDITOR + ref UnityDebugName nameComponent = ref world.GetPool().Add(result); + nameComponent.DisplayName = entityName; + nameComponent.changed = true; +#endif + return result; + } + + /// + /// Sets the debug name shown for an entity in the debug hierarchy, if running + /// inside the unity editor. (In builds, this is ignored) + /// + public static void UpdateEntityName(this EcsWorld world, int entity, string entityName) + { +#if UNITY_EDITOR + var pool = world.GetPool(); + ref UnityDebugName nameComponent = ref pool.GetOrCreate(entity); + nameComponent.DisplayName = entityName; + nameComponent.changed = true; +#endif + } + + + private static ref T GetOrCreate(this EcsPool self, int entity) where T : struct + { + if (!self.Has(entity)) + { + return ref self.Add(entity); + } + + return ref self.Get(entity); + } + } +} \ No newline at end of file diff --git a/Runtime/UnityDebugName.cs.meta b/Runtime/UnityDebugName.cs.meta new file mode 100644 index 0000000..82c5ae9 --- /dev/null +++ b/Runtime/UnityDebugName.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 67fd943a82054163ae5634d943a0c55d +timeCreated: 1624128570 \ No newline at end of file