Skip to content

Instantly share code, notes, and snippets.

@shana
Created July 23, 2020 14:52
Show Gist options
  • Save shana/14992f3b5a0e2130ef27c3acb9316504 to your computer and use it in GitHub Desktop.
Save shana/14992f3b5a0e2130ef27c3acb9316504 to your computer and use it in GitHub Desktop.
Writing debug data in the Entity Debugger
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
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
}
/// <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) });
}
});
}
}
/// <summary>
/// Updates entity name based on <see cref="DebugName"/> component.
/// </summary>
[ExecuteAlways, UpdateInGroup(typeof(InitializationSystemGroup))]
public class DebugNameSystem : ComponentSystem
{
public static string PrefabPrefix = "☒ ";
private static readonly EntityQueryDesc queryDesc = new EntityQueryDesc { All = new[] { ComponentType.ReadOnly<DebugName>() }, Options = EntityQueryOptions.IncludeDisabled };
private static readonly EntityQueryDesc 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(queryDesc);
m_query.SetChangedVersionFilter(typeof(DebugName));
m_queryPrefabs = GetEntityQuery(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());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment