Drawing Text In Scene View With Handles.

Unity 2021.3.8f1:
Pasteboard - Uploaded Image

I have a script “Assets\Editor\SphereLoadEditor.cs”

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(SphereLoad))]
public class SphereLoadEditor : Editor
{
    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
      
    }

    void OnSceneGUI()
    {
        SphereLoad t = (SphereLoad)target;

        GUIStyle style = new GUIStyle();
        style.fontSize = 1;
        style.normal.textColor = Color.red;
        Vector3 tpos = t.transform.position;
        tpos.x = tpos.x + 2.0f;
        tpos.y = tpos.y + 2.0f;
        tpos.z = tpos.z + 2.0f;
        Handles.Label(tpos, "Hello World!", style);
    }
}

I have another script “Assets\SphereLoad.cs”

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class SphereLoad : MonoBehaviour
{
    GameObject mGameObject;
    // Start is called before the first frame update
    void Start()
    {
          
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}

There are no errors or warnings present in the Unity console error when I run the scene. I do not see any labels showing up in the scene at all. For some reason I am not able to get a breakpoint to trigger in the editor script or get the autocomplete working in Visual Studio however the script does seem to be processed by Unity as I had to fix a few script issues in order to get the scene to start up. So far I would like to check the status of the position coming into the editor script I also tried to manually enter a vector position for a Handles.Label such as:

Vector3 tv = new Vector3(2.0f, 2.0f, 2.0f);
Handles.Label(tv, "Hello World!", style);

That also did not seem to draw a label in scene view. Is there a way to get the scene view labels to start to show up in scene view?

Make sure to wrap up your GUI code with Handles.BeginGUI/EndGUI.

This is a slightly different approach (with GUI.Label), but does a similar thing.

static public void DrawText(string text, Vector3 worldPos, Vector2 screenOffset = default, Color? color = default, int alignment = 0) {
  UnityEditor.Handles.BeginGUI();

  var restoreColor = GUI.color;
  if(color.HasValue) GUI.color = color.Value;

  var view = UnityEditor.SceneView.currentDrawingSceneView;
  var screenPos = view.camera.WorldToScreenPoint(worldPos);
  screenPos += new Vector3(screenOffset.x, screenOffset.y, 0f);

  if(screenPos.y < 0f || screenPos.y > Screen.height || screenPos.x < 0f || screenPos.x > Screen.width || screenPos.z < 0f) {
    GUI.color = restoreColor;

  } else {
    var size = GUI.skin.label.CalcSize(new GUIContent(text));

    if(alignment == 0) {
      screenPos.x -= (size.x / 2f) + 1f;
    } else if(alignment < 0) {
      screenPos.x -= size.x - 2f;
    } else {
      screenPos.x -= 4f;
    }

    GUI.Label(new Rect(screenPos.x, -screenPos.y + view.position.height + 4f, size.x, size.y), text);
    GUI.color = restoreColor;

  }

  UnityEditor.Handles.EndGUI();
}

That’s what I thought as well. However the Handles.Label method already does this. So if it’s not visible, it’s most likely not in the sceneview camera view.

Though another issue could be this line:
GUIStyle style = new GUIStyle();

Creating a new style from scratch could miss some crucial settings like the font. In most cases you want to duplicate an existing style. You can pass an existing style as parameter to the GUIStyle constructor to get a cloned version.

Try

GUIStyle style = new GUIStyle("label");

instead

1 Like

Just wanted to add that you can also try using EditorStyles.label, same thing, more like a heads-up that there is a collection of commonly-used styles in one place. (edit: or just leave the style out, because according to doc, ‘label’ is automatically used if the style is missing.)

1 Like

Oh, and maybe your gizmos are off in the scene view?

1 Like