Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Shiolle · Jan 27, 2011 at 04:01 PM · editor-scriptingdrawline

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));

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shiolle · Jan 28, 2011 at 01:31 PM 0
Share

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.

avatar image Shiolle · Jan 28, 2011 at 02:30 PM 0
Share

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.

avatar image
-1

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".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shiolle · Jan 27, 2011 at 05:15 PM 1
Share

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.

avatar image
0

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/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 16, 2017 at 11:52 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to create persistent handles in Editor Scene View? 1 Answer

Custom asset icons changed after upgrade to Unity 2017.2 0 Answers

How to create enum out of array of string? 2 Answers

I want teleport exit which it does specific Colider2D and to transport you to a specific one target how do I do that? 1 Answer

How to communicate between two editor scripts? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges