D
D
Dmitry2016-12-03 05:24:23
Unity
Dmitry, 2016-12-03 05:24:23

Why does the editor window start to slow down because of DrawGizmo?

Hello dear experts.
I'm writing a custom tile drawer. I have the following code for drawing a grid in the editor screen by tile size:

[CustomEditor(typeof(GameObject))]
  public class SceneGUITest : Editor
  {		
    [DrawGizmo(GizmoType.NotInSelectionHierarchy)]		
    static void RenderCustomGizmo(Transform objectTransform, GizmoType gizmoType)
    {
      if (isEnabled && isGrid)
      {				
        Gizmos.color = Color.white;				

        Vector3 minGrid = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(new Vector2(0f, 0f)).origin;
        Vector3 maxGrid = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(new Vector2(SceneView.currentDrawingSceneView.camera.pixelWidth, SceneView.currentDrawingSceneView.camera.pixelHeight)).origin;

        for (float i = Mathf.Round(minGrid.x / gridSize.x) * gridSize.x; i < Mathf.Round(maxGrid.x / gridSize.x) * gridSize.x && gridSize.x > 0.05f; i+=gridSize.x)
          Gizmos.DrawLine(new Vector3(i,minGrid.y,0.0f), new Vector3(i,maxGrid.y,0.0f));
        for (float j = Mathf.Round(minGrid.y / gridSize.y) * gridSize.y; j < Mathf.Round(maxGrid.y / gridSize.y) * gridSize.y && gridSize.y > 0.05f; j+=gridSize.y)
          Gizmos.DrawLine(new Vector3(minGrid.x,j,0.0f), new Vector3(maxGrid.x,j,0.0f));
        SceneView.RepaintAll();

      }
    }

The problem is that while there are a few drawn (object) tiles on the scene, there are few brakes, and when the number is somewhere more than two hundred objects, the Unity editor itself starts to slow down, as soon as you turn off the grid, the brakes disappear abruptly. I can't understand the relationship between the number of objects in the scene and the presence of the grid on the screen.
Tell me what could be the problem?
Upd:
Maybe it's not because of DrawGizmo, I don't know, but I found another dependency - when all objects in the editor scene are turned off and on again, the brakes disappear, but not for long, over time, literally within a minute, the brakes appear again.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hightlander, 2016-12-05
@Dimusikus

It's very simple:
The method with the attribute "[DrawGizmo(GizmoType.NotInSelectionHierarchy)]" is called for each unselected object in the scene. The more objects, the more often it is called, which is the reason for the brakes.
PS So far I have not found an elegant way to get a single method call. But as an option, you can create an object with the HideFlags.HideAndDontSave flag and draw through it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question