Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Constant screen-size gizmos?

Discussion in 'Editor & General Support' started by duke, Oct 17, 2010.

  1. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    764
    How do I go about ensuring, say, Gizmos.DrawWireCube is a fixed size relative to the viewport camera?
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    I'm not sure what you want to do; gizmos are drawn in world space, so their size is going to change relative to how far away they are. If you want a gizmo to appear the same size on screen regardless of where it is in world space, you'll need to how far away the camera is and scale the gizmo accordingly.
     
  3. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    86
    I know this is REALLY old, but it's still the first thing that comes up in Google when searching for screen size gizmos.

    HandleUtility.GetHandleSize(worldPos) does exactly what you want, but unfortunately it's only available in the editor...

    But then I realized I could just use ILSpy to check out what GetHandleSize does, and rewrote it to use Gizmos matrix, and now it works great. So here it is:


    Code (CSharp):
    1.  
    2.  
    3.  
    4.     public static float GetGizmoSize(Vector3 position)
    5.     {
    6.         Camera current = Camera.current;
    7.         position = Gizmos.matrix.MultiplyPoint(position);
    8.  
    9.         if (current)
    10.         {
    11.             Transform transform = current.transform;
    12.             Vector3 position2 = transform.position;
    13.             float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f)));
    14.             Vector3 a = current.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z)));
    15.             Vector3 b = current.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z)));
    16.             float magnitude = (a - b).magnitude;
    17.             return 80f / Mathf.Max(magnitude, 0.0001f);
    18.         }
    19.  
    20.         return 20f;
    21.     }
    22.  
    23.  
     
    Last edited: Oct 31, 2014
    metroidsnes likes this.
  4. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    505