- Home /
How to clear lines form Debug.DrawLine?
I need to write a custom editor tools for my application. One of those tools should draw a 2d grid inside the scene. I use Debug.DrawLine to do it. The grid should also redraw if user changes the grid parameters in my tool. Yet, I can't seem to find the function to clear already drawn lines. How can I clear already drawn lines? I think it would be beneficial to provide a code. Note that Im' not redrawing it here every frame. It only draws on button stroke.
public void OnGUI() {
...............................................................................
if(GUILayout.Button("Map world", GuiLOpt)) { DrawGrid(); }
}
protected void DrawGrid()
{
HandleUtility.Repaint();
float XShift, ZShift;
XShift = cellSize * ((float)cellNumX) / 2;
ZShift = cellSize * ((float)cellNumY) / 2;
for(int i = 0; i <= cellNumY; i++)
Debug.DrawLine(new Vector3(-XShift, 0, -ZShift + i * cellSize), new Vector3(XShift, 0, -ZShift + i * cellSize));
for(int i = 0; i <= cellNumX; i++)
Debug.DrawLine(new Vector3(-XShift + i * cellSize, 0, -ZShift), new Vector3(-XShift + i * cellSize, 0, ZShift));
}
Answer by TowerOfBricks · Jan 27, 2011 at 07:54 PM
For editor scripts you should use Handles instead (or possibly Gizmos if that's possible (Handles are usually better though)).
Drawing should be done in the OnSceneGUI function
Thank you, I'm still trying to understand how to use handles properly. So far I could only see them drawing directly in a tool window, nut not in the scene.
It appears that OnSceneGUI function is never called if you derive EditorWindow class. I can't use CustomInspector either because there is nothing to inspect yet when I launch the tool.
Answer by Jessy · Jan 27, 2011 at 04:32 PM
Debug.DrawLine only draws the lines that you specifically tell it to draw, and only on the frames you tell it to draw them. There is no way to "clear" them, because they don't "stick around".
That's what I thought too, but unfortunately they do stick around. Since it's an editor scrip I'm talking about the editor of course. They stick around even when I close my tool completely.
Answer by TimKofoed · Aug 10, 2012 at 11:55 AM
I also had the same need for a 2D grid, but after trying (and failing) to wrap my head around OnSceneGUI, OnInspectorGUI, Handles, etc. I finally found this tutorial which very quickly and easily draws a 2D grid using Gizmo.drawLines.
During my attempts with Debug.DrawLine, it drew a lot of power from the editor because I must have been rendering the lines far too many times per frame, but this Gizmo-way of doing it seems a lot more efficient.
http://active.tutsplus.com/tutorials/workflow/how-to-add-your-own-tools-to-unitys-editor/
Answer by dwitee · Oct 16, 2017 at 10:15 AM
Of course there is no function to clear the Debug.Drawline but if you pass duration as Time.deltaTime then there will be no multiple draw of the line if you drawing in every frame.
Debug.DrawLine(start, end, Color.white, Time.deltaTime);
This is pointless at edit time. Passing deltaTime makes no sense. As you can read on the documentation page when the duration is 0 (which is the default) the line is only drawn for one frame. However since the editor does not update at regular frames the line would stick around until they get "cleared".
Lines drawn at edit time with a duration of "0" are cleared when you enter playmode. Lines drawn at edit time with a duration greater than one are cleared when you enter and leave playmode.
As it has been said by others you shouldn't use Debug.DrawLine during edit time. At runtime Debug,DrawLine works just fine. You don't have to pass any duration when you want to draw it for one frame as this happens automatically. However the question wasn't about the usage at runtime.
Your answer
Follow this Question
Related Questions
How to create persistent handles in Editor Scene View? 1 Answer
Is it possible to detect changes to a different component within an editor script? 0 Answers
Copying files from one directory to the other 1 Answer
Is it possible to draw more than one property attribute on a field? 1 Answer
Browse button in unity editor script 0 Answers