[HowTo] ECS Entity names from GameObjects

I struggle a lot with ECS debugger, because it’s hard to find entities.
I’ve created these scripts to display names and distinguish prefabs, feel free to use it.

using Unity.Collections;
using Unity.Entities;

public struct DebugName : IComponentData
{
    public NativeString64 Value;

#if UNITY_EDITOR
    // To make this work, it requires EntityComponentInspector: [URL]https://github.com/OndrejPetrzilka/EntityComponentInspector[/URL]
    void OnEditorGUI(string label)
    {
        Value = new NativeString64(UnityEditor.EditorGUILayout.TextField(label, Value.ToString()));
    }
#endif
}
using Unity.Collections;
using Unity.Entities;
using UnityEngine;

/// <summary>
/// Adds <see cref="DebugName"/> to all entities.
/// </summary>
[UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
public class DebugNameConversionSystem : GameObjectConversionSystem
{
    protected override void OnUpdate()
    {
        Entities.ForEach((Transform transform) =>
        {
            var entity = GetPrimaryEntity(transform);
            if (entity != Entity.Null)
            {
                DstEntityManager.AddComponentData(entity, new DebugName { Value = new NativeString64(transform.gameObject.name) });
            }
        });
    }
}
using Unity.Entities;
using UnityEngine;

/// <summary>
/// Updates entity name based on <see cref="DebugName"/> component.
/// </summary>
[ExecuteAlways, UpdateInGroup(typeof(InitializationSystemGroup))]
public class DebugNameSystem : ComponentSystem
{
    public static string PrefabPrefix = "☒ ";
    static EntityQueryDesc m_queryDesc = new EntityQueryDesc { All = new[] { ComponentType.ReadOnly<DebugName>() }, Options = EntityQueryOptions.IncludeDisabled };
    static EntityQueryDesc m_queryDescPrefabs = new EntityQueryDesc { All = new[] { ComponentType.ReadOnly<DebugName>(), ComponentType.ReadOnly<Prefab>() }, Options = EntityQueryOptions.IncludeDisabled | EntityQueryOptions.IncludePrefab };

    EntityQuery m_query;
    EntityQuery m_queryPrefabs;

    protected override void OnCreate()
    {
        base.OnCreate();
        m_query = GetEntityQuery(m_queryDesc);
        m_query.SetChangedVersionFilter(typeof(DebugName));

        m_queryPrefabs = GetEntityQuery(m_queryDescPrefabs);
        m_queryPrefabs.SetChangedVersionFilter(typeof(DebugName));
    }

    protected override void OnUpdate()
    {
        Entities.With(m_query).ForEach((Entity entity, ref DebugName name) =>
        {
            EntityManager.SetName(entity, name.Value.ToString());
        });

        Entities.With(m_queryPrefabs).ForEach((Entity entity, ref DebugName name) =>
        {
            EntityManager.SetName(entity, PrefabPrefix + name.Value.ToString());
        });
    }
}
13 Likes

this is refreshing

1 Like

NativeString64 is deprecated since Unity Collections Package V0.11.0 and was removed in V0.14.0. Use FixedString64 now and it works.

Use DOTs editor package, it has much better support for inspecting entities

1 Like

For some reason I didn’t even know about this. Thanks for posting!

1 Like