Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Quick question: Drawing Gizmos in Scene View in Edit Mode

Discussion in 'Editor & General Support' started by NDSno1, Mar 15, 2018.

  1. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    Hi all,
    I'm trying to draw Gizmos in scene view for debugging, but the gizmos were only drawn when in play mode, and in edit mode they didn't show up. Here is my code:
    Code (CSharp):
    1.  void OnDrawGizmosSelected()
    2.     {
    3.  
    4. #if UNITY_EDITOR
    5.         Gizmos.color = GizmoColor;
    6.  
    7.         //Draw the suspension
    8.         Gizmos.DrawLine(
    9.             transform.position - m_dummyWheel.up * m_wheelRadius,
    10.             transform.position + (m_dummyWheel.up * (m_suspensionDistance - m_suspensionCompression))
    11.         );
    12.  
    13.         //Draw the wheel
    14.         Vector3 point1;
    15.         Vector3 point0 = transform.TransformPoint(m_wheelRadius * new Vector3(0, Mathf.Sin(0), Mathf.Cos(0)));
    16.         for (int i = 1; i <= 20; ++i)
    17.         {
    18.             point1 = transform.TransformPoint(m_wheelRadius * new Vector3(0, Mathf.Sin(i / 20.0f * Mathf.PI * 2.0f), Mathf.Cos(i / 20.0f * Mathf.PI * 2.0f)));
    19.             Gizmos.DrawLine(point0, point1);
    20.             point0 = point1;
    21.  
    22.         }
    23.  
    24.         //draw force application point
    25.         Gizmos.DrawWireSphere(forcePoint, 0.05f);
    26.  
    27.         Gizmos.color = Color.white;
    28. #endif
    29.     }
    This script inherits MonoBehavior, and was attached to a game object that had mash renderer. I think the problem might be because the script is attached to a game object with mesh renderer, so the gizmos of the script was drawn.

    Thank you very much.
     
  2. It works for me in this simplified form:
    Code (CSharp):
    1.     void OnDrawGizmosSelected()
    2.     {
    3.  
    4. #if UNITY_EDITOR
    5.         Gizmos.color = Color.red;
    6.  
    7.         //Draw the suspension
    8.         Gizmos.DrawLine(
    9.             Vector3.zero,
    10.             Vector3.up
    11.         );
    12.  
    13.         //draw force application point
    14.         Gizmos.DrawWireSphere(Vector3.zero, 0.05f);
    15.  
    16.         Gizmos.color = Color.white;
    17. #endif
    18.     }
    Are you sure, you selected the game object in the editor? (You're using the OnDrawGizmosSelected)
    Are you sure that your math is okay? I would Debug.Log these numbers and check if it isn't somewhere else.
    Like transform.position - m_dummyWheel.up * m_wheelRadius and transform.position + (m_dummyWheel.up * (m_suspensionDistance - m_suspensionCompression)).
     
  3. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    I selected the game object, that's why I used OnDrawGizmosSelected, I'm not that dumb :p
    Just tested the simplified form, still didnt work. Selected the object, no gizmos.
    Or do I need a separate Editor script for displaying in Edit Mode, and OnDrawGizmosSelected is only for play mode?
     
  4. I don't think that you're dumb, I think you're human (I hope at least) and humans make mistakes. Me included.

    What I have posted works out of the box in edit mode for me in 2017.3.1.

    Also you mentioned that you're selecting a game object with mesh renderer...
    Does it shows its mesh? Is it possible that the gizmo is underneath the mesh (small)? Have you tried to attach this to cube? Have you tried to check out in wireframe mode?
     
    NDSno1 likes this.
  5. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    The wireframe for the mesh was drawn fine.
    The gizmos was visible in play mode, so there is no way visibility was the problem.
    I used the simple code again and it works fine when attaching to the cube. But I have to go to play mode once and exit for it to work. Attaching the simple code to the game object works. It just the code i'm using didn't work, so I think there might be some uninitialized variable so the gizmos can't be drawn.
     
  6. Marzoa

    Marzoa

    Joined:
    Dec 2, 2012
    Posts:
    50
    Did you manage to solve this in the last six years? Is there really a way to draw gizmos in the Scene View WITHOUT running the game?

    Thanks!
     
  7. senwuab

    senwuab

    Joined:
    Mar 30, 2020
    Posts:
    1
    lurkin ninja's answer has no problem,that works.
     
  8. wisher_wisher

    wisher_wisher

    Joined:
    Jul 23, 2018
    Posts:
    3
    after some researching here it is:

    you need to create a new code only for drawlines
    1. Delete update and start method.
    2. use one of these OnDrawGizmos OR OnDrawGizmosSelected methods (make sure you will declare variables inside the method).
    3. inside Your Class Use this attribute [ExecuteInEditMode].
    4. W8 for unity to compile.
    5. and Enjoy.

    here is a Sample :
    Code (CSharp):
    1. public class DrawLine : MonoBehaviour
    2. {
    3.     [ExecuteInEditMode]
    4.      
    5.     private void OnDrawGizmos()
    6.     {
    7.         GameObject pa;
    8.         pa = GameObject.Find("AngryGrandPa");
    9.  
    10.         Gizmos.color = Color.red;
    11.         Gizmos.DrawLine(this.gameObject.transform.position, pa.transform.position);
    12.     }
    13. }
     
  9. SatansPineapple

    SatansPineapple

    Joined:
    Mar 6, 2020
    Posts:
    1
    I don t know whether it helps but the Vector 3 have to known.
    If the Position would be calculated at runtime, the Gizmos are not painted in Edit Mode.
    Or rather the Gizmos where painted at position 0,0,0 if u are lucky.