1
\$\begingroup\$

I want to show some label on some rect transforms using Handles.Label(). it shows the text with a default font size regardless of the zoom level in the scene window.

public class NodeWrapper : MonoBehaviour {

    public Node node;

    void OnDrawGizmos()
    {
        Handles.Label(transform.position + new Vector3(-.9f, .5f), node.Name);
        Handles.Label(transform.position + new Vector3(-.9f, .4f), node.Info);
        ...
    }
}

So when I zoom out in the scene window, the labels get closer and at some point they become unreadable. I want to show/hide some lines when zoom level changes. How can I achieve that? Is it possible to get the current zoom level of the scene?

edit

PS. I'm developing a node editor for another game, I use scene window to create and serialize some objects. My target is only Scene window which uses 2D config.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ how are you handling zoom? perspective or orthographic camera? \$\endgroup\$ Commented Jul 3, 2016 at 18:49
  • \$\begingroup\$ I forgot to mention it's a 2D scene so it's orthographic. \$\endgroup\$
    – Bizhan
    Commented Jul 4, 2016 at 7:32
  • \$\begingroup\$ what kind of objects are the ones that hold the text? \$\endgroup\$ Commented Jul 4, 2016 at 17:56
  • \$\begingroup\$ if you mean the rectangles they are UI.Image. but the text is being drawn using Handles.Label() \$\endgroup\$
    – Bizhan
    Commented Jul 5, 2016 at 14:08

3 Answers 3

3
\$\begingroup\$

To get the Handles.Label function to work in different zoom levels you need to change two things:

  1. Use the function signature that accepts GUIStyle
  2. Calculate the fontSize while considering the orthographicSize of the scene camera.

For example:

// this is the internal camera rendering the scene view, not the main camera!
float zoom = SceneView.currentDrawingSceneView.camera.orthographicSize;

// the style object allows you to control font size, among many other settings
var style = new GUIStyle();

// this value depends on your scene, tweak it to match the other objects
int fontSize = 70; 

// as you zoom out, the ortho size actually increases, 
// so dividing by it makes the font smaller which is exactly what we need
style.fontSize = Mathf.FloorToInt(fontSize / zoom);

Handles.Label(transform.position + new Vector3(-.9f, .5f), node.Name, style);
\$\endgroup\$
1
\$\begingroup\$

docs.unity3d.com/ScriptReference/Camera-orthographicSize.html , orthographicSize value is your zoom, make a script to get that value from the main.camera and manipulate the labels according to that value.

small example,

public Camera cam;
private float orthoZoom;

void Update() {
  orthoZoom = cam.orthographicSize;
}
\$\endgroup\$
4
  • \$\begingroup\$ thank you but my scene doesn't require a camera. I don't run my project I just use the scene window to create and serialize some gameobjects. \$\endgroup\$
    – Bizhan
    Commented Jul 4, 2016 at 13:33
  • \$\begingroup\$ yea I understood that, but by default every scene that will use your editor script will have a Camera set on ortographic otherwise the scene wont be visible. so you just have to check if there is a camera and if its true then your editor can work. \$\endgroup\$ Commented Jul 4, 2016 at 13:44
  • \$\begingroup\$ as I said there is no camera in the scene. I added a picture to demonstrate my goal. \$\endgroup\$
    – Bizhan
    Commented Jul 4, 2016 at 17:52
  • 2
    \$\begingroup\$ There's no camera in the scene, but there is a hidden camera rendering the editor's scene view. You can get a reference to it in OnDrawGizmos (and maybe other methods) using UnityEditor.SceneView.currentDrawingSceneView.camera \$\endgroup\$
    – DMGregory
    Commented Aug 5, 2016 at 4:55
0
\$\begingroup\$

I tested, and it worked!

float zoom = SceneView.currentDrawingSceneView.size;
float fontSize = 300f; 
var style = new GUIStyle();
style.fontSize = Mathf.FloorToInt(fontSize / zoom);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .