Skip to content

Commit

Permalink
Added manual naming to the new ecslite-unityeditor
Browse files Browse the repository at this point in the history
  • Loading branch information
RealityStop committed Jun 20, 2021
1 parent ebd625a commit 4d2d0cb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
42 changes: 33 additions & 9 deletions Runtime/EcsWorldDebugSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed class EcsWorldDebugSystem : IEcsPreInitSystem, IEcsRunSystem, IEcs
EcsEntityDebugView[] _entities;
Dictionary<int, byte> _dirtyEntities;
Type[] _typesCache;
private EcsPool<UnityDebugName> _namePool;
private EcsFilter _debugNameFilter;

public EcsWorldDebugSystem (string worldName = null, bool bakeComponentsInName = true) {
_bakeComponentsInName = bakeComponentsInName;
Expand All @@ -38,21 +40,43 @@ public void PreInit (EcsSystems systems) {
_entities = new EcsEntityDebugView [_world.GetWorldSize ()];
_dirtyEntities = new Dictionary<int, byte> (_entities.Length);
_world.AddEventListener (this);
if (!_bakeComponentsInName)
{
_namePool = _world.GetPool<UnityDebugName>();
_debugNameFilter = _world.Filter<UnityDebugName>().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) {
Expand Down
55 changes: 55 additions & 0 deletions Runtime/UnityDebugName.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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)
/// </summary>
public static int NewEntity(this EcsWorld world, string entityName)
{
int result = world.NewEntity();
#if UNITY_EDITOR
ref UnityDebugName nameComponent = ref world.GetPool<UnityDebugName>().Add(result);
nameComponent.DisplayName = entityName;
nameComponent.changed = true;
#endif
return result;
}

/// <summary>
/// Sets the debug name shown for an entity in the debug hierarchy, if running
/// inside the unity editor. (In builds, this is ignored)
/// </summary>
public static void UpdateEntityName(this EcsWorld world, int entity, string entityName)
{
#if UNITY_EDITOR
var pool = world.GetPool<UnityDebugName>();
ref UnityDebugName nameComponent = ref pool.GetOrCreate(entity);
nameComponent.DisplayName = entityName;
nameComponent.changed = true;
#endif
}


private static ref T GetOrCreate<T>(this EcsPool<T> self, int entity) where T : struct
{
if (!self.Has(entity))
{
return ref self.Add(entity);
}

return ref self.Get(entity);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/UnityDebugName.cs.meta

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

0 comments on commit 4d2d0cb

Please sign in to comment.