Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[HowTo] ECS Entity names from GameObjects

Discussion in 'Entity Component System' started by OndrejP, Dec 21, 2019.

  1. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    296
    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.

    upload_2019-12-21_15-34-58.png

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3.  
    4. public struct DebugName : IComponentData
    5. {
    6.     public NativeString64 Value;
    7.  
    8. #if UNITY_EDITOR
    9.     // To make this work, it requires EntityComponentInspector: [URL]https://github.com/OndrejPetrzilka/EntityComponentInspector[/URL]
    10.     void OnEditorGUI(string label)
    11.     {
    12.         Value = new NativeString64(UnityEditor.EditorGUILayout.TextField(label, Value.ToString()));
    13.     }
    14. #endif
    15. }
    16.  
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using UnityEngine;
    4.  
    5. /// <summary>
    6. /// Adds <see cref="DebugName"/> to all entities.
    7. /// </summary>
    8. [UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
    9. public class DebugNameConversionSystem : GameObjectConversionSystem
    10. {
    11.     protected override void OnUpdate()
    12.     {
    13.         Entities.ForEach((Transform transform) =>
    14.         {
    15.             var entity = GetPrimaryEntity(transform);
    16.             if (entity != Entity.Null)
    17.             {
    18.                 DstEntityManager.AddComponentData(entity, new DebugName { Value = new NativeString64(transform.gameObject.name) });
    19.             }
    20.         });
    21.     }
    22. }
    23.  
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3.  
    4. /// <summary>
    5. /// Updates entity name based on <see cref="DebugName"/> component.
    6. /// </summary>
    7. [ExecuteAlways, UpdateInGroup(typeof(InitializationSystemGroup))]
    8. public class DebugNameSystem : ComponentSystem
    9. {
    10.     public static string PrefabPrefix = "☒ ";
    11.     static EntityQueryDesc m_queryDesc = new EntityQueryDesc { All = new[] { ComponentType.ReadOnly<DebugName>() }, Options = EntityQueryOptions.IncludeDisabled };
    12.     static EntityQueryDesc m_queryDescPrefabs = new EntityQueryDesc { All = new[] { ComponentType.ReadOnly<DebugName>(), ComponentType.ReadOnly<Prefab>() }, Options = EntityQueryOptions.IncludeDisabled | EntityQueryOptions.IncludePrefab };
    13.  
    14.     EntityQuery m_query;
    15.     EntityQuery m_queryPrefabs;
    16.  
    17.     protected override void OnCreate()
    18.     {
    19.         base.OnCreate();
    20.         m_query = GetEntityQuery(m_queryDesc);
    21.         m_query.SetChangedVersionFilter(typeof(DebugName));
    22.  
    23.         m_queryPrefabs = GetEntityQuery(m_queryDescPrefabs);
    24.         m_queryPrefabs.SetChangedVersionFilter(typeof(DebugName));
    25.     }
    26.  
    27.     protected override void OnUpdate()
    28.     {
    29.         Entities.With(m_query).ForEach((Entity entity, ref DebugName name) =>
    30.         {
    31.             EntityManager.SetName(entity, name.Value.ToString());
    32.         });
    33.  
    34.         Entities.With(m_queryPrefabs).ForEach((Entity entity, ref DebugName name) =>
    35.         {
    36.             EntityManager.SetName(entity, PrefabPrefix + name.Value.ToString());
    37.         });
    38.     }
    39. }
    40.  
     
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,994
    this is refreshing
     
    deus0 likes this.
  3. jg482

    jg482

    Joined:
    Apr 5, 2018
    Posts:
    8
    NativeString64 is deprecated since Unity Collections Package V0.11.0 and was removed in V0.14.0. Use FixedString64 now and it works.
     
  4. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Use DOTs editor package, it has much better support for inspecting entities
     
    Last edited: Jun 29, 2021
    laurentlavigne likes this.
  5. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    449
    For some reason I didn't even know about this. Thanks for posting!
     
    Neiist likes this.