Entities - debug entity selected in editor

Hello,

I would like to achieve the following behavior:

  1. In the editor’s Entities Hierarchy window, I select an entity.
  2. If the selected entity has the desired component, I want to draw debug information. For example, I want to draw custom gizmos on entity’s position.

Is it possible to achieve this?

I experimented a bit with the Selection class and managed to get the selected InstanceIDs and objects. However, I’m unsure whether this information is useful for obtaining the chosen entity’s Index and version.

I believe that if I can access the Index and version of the entity, I will be able to achieve my goal using a hybrid approach.

Thank you in advance for your help!

Ok, so I found a solution in case anybody is interested. When using Selection.objects on entities in the editor, the selected objects have the type Unity.Entities.Editor.EntitySelectionProxy. Unfortunately, this type is inaccessible from the outside, so we need to use some reflection magic to obtain the selected entityIndex and entityVersion:

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Reflection;

public class GetSelectedEntityInfo : MonoBehaviour
{
    void Update()
    {
        if (Selection.objects != null && Selection.objects.Length > 0)
        {
            foreach (Object obj in Selection.objects)
            {
                if (obj.GetType().Name == "EntitySelectionProxy")
                {
                    // Use reflection to access private fields
                    FieldInfo entityIndexField = obj.GetType().GetField("entityIndex", BindingFlags.NonPublic | BindingFlags.Instance);
                    FieldInfo entityVersionField = obj.GetType().GetField("entityVersion", BindingFlags.NonPublic | BindingFlags.Instance);

                    if (entityIndexField != null && entityVersionField != null)
                    {
                        int entityIndex = (int)entityIndexField.GetValue(obj);
                        int entityVersion = (int)entityVersionField.GetValue(obj);

                        Debug.Log($"Selected entity with index {entityIndex} and version {entityVersion}");
                    }
                }
            }
        }
    }
}
#endif