Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Debug.DrawLine thickness

Discussion in 'Scripting' started by jessee03, May 28, 2022.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Is there a way to make Debug.Drawline thicker?
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Not really. Why is that important? They are just visible inside the editor as they are pure debugging tools.
     
  3. MW_MichaelWolf95

    MW_MichaelWolf95

    Joined:
    May 23, 2018
    Posts:
    3
    I created a static "DebugDraw" utility class for this, and other similar tools.

    Here's my solution:

    Code (CSharp):
    1. // Static editor utility class for drawing.
    2. public static class DebugDraw
    3. {
    4.  
    5. // Last known pixel-ratio of the scene view camera. This represents the world-space distance between each pixel.
    6. // This cache gets around an issue where the scene view camera is null on certain editor frames.
    7. private static float lastKnownSceneViewPixelRatio = 0.001f;
    8.  
    9. // Draws a line in the scene
    10. public static void DrawLine(Vector3 start, Vector3 end, float thickness, Color color, float duration = 0f)
    11. {
    12. #if UNITY_EDITOR
    13.     Vector3 lineDir = (end - start).normalized;
    14.     Vector3 toCamera = Vector3.back;
    15.  
    16.     Camera camera = null;
    17.     // Get the current scene view camera.
    18.     if (SceneView.currentDrawingSceneView != null)
    19.     {
    20.         camera = SceneView.currentDrawingSceneView.camera;
    21.     }
    22.    
    23.     if (camera == null)
    24.     {
    25.         // Default to the
    26.         camera = Camera.current;
    27.     }
    28.     if (camera != null)
    29.     {
    30.         toCamera = (camera.transform.position - start).normalized;
    31.         lastKnownSceneViewPixelRatio = (camera.orthographicSize * 2f) / camera.pixelHeight;
    32.     }
    33.    
    34.     Vector3 orthogonal = Vector3.Cross(lineDir, toCamera).normalized;
    35.     int pixelThickness = Mathf.CeilToInt(thickness);
    36.     float totalThick = lastKnownSceneViewPixelRatio * pixelThickness;
    37.     for(int i = 0; i < pixelThickness; i++)
    38.     {
    39.         Vector3 offset = orthogonal * ((i * lastKnownSceneViewPixelRatio) - (totalThick/2f));
    40.         Debug.DrawLine(start + offset, end + offset, color, duration);
    41.     }
    42. #endif //UNITY_EDITOR
    43. }
    44.  
    45. }
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    You can also adapt LineRenderer for the editor if you really want things to pop out.
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Wow I never noticed Handles.DrawLine had thickness. Is it any good? Now I have to try it.
    I like pretty gizmos, almost 60% of my work is in gizmos.

    Edit:
    I've just seen that image on unity docs FOR THE FIRST TIME EVER, that's crazy
    who knows what else I'm missing out on
     
  7. brunopg

    brunopg

    Joined:
    May 10, 2016
    Posts:
    2
    You may not be crazy. Handles line thickness was introduced in Unity 2021.
     
    orionsyndrome likes this.