Just a quick question. Im a huge fan on data visualizing in the viewport eg. Debug.DrawLine and OnDrawGizmos to see my data. Is it ok for debugging reasons to get entities into a MonoBehaviour and draw them in OnDrawGizmos, or is there a better way? How would I get entities into an old school MonoBehaviour ? Thanks in advance
There’s another way which you might find nicer, although it’s a little bit of a hack and I’ve only done very minimal testing with it, so I don’t know how well it holds up.
Unity provides a DrawGizmo attribute which can be applied to a static method in any class to draw gizmos. We can use it like so.
static class GizmoInvoker
{
private static float doneTime;
[DrawGizmo(GizmoType.NonSelected)]
private static void DrawSystemGizmos(MonoBehaviour obj, GizmoType gizmoType)
{
if (Time.time == doneTime) return;
doneTime = Time.time;
foreach (var world in Unity.Entities.World.AllWorlds)
foreach (var system in world.Systems)
if (system is IGizmoDrawing gizmoDrawingSystem) gizmoDrawingSystem.DrawGizmos();
}
}
interface IGizmoDrawing
{
void DrawGizmos();
}
This should find all active systems which have the IGizmoDrawing interface and call the method on each. The reason it’s a bit of a hack is that the attribute method is called once for each Monobehaviour in the scene. We can keep track of the time to make it run only once per frame, but if there are no Monobehaviours it won’t run.
If anyone from Unity’s reading this, it would be nice to allow the DrawGizmo attribute to be applied to a method with no parameters and to have it run once per frame.
I think I’ve seen advice given by Unity people saying that we should never put any code in a ComponentSystemGroup, but this does look like a nice way to do it.