-
-
Save Arakade/9dd844c2f9c10e97e3d0 to your computer and use it in GitHub Desktop.
static void drawString(string text, Vector3 worldPos, Color? colour = null) { | |
UnityEditor.Handles.BeginGUI(); | |
if (colour.HasValue) GUI.color = colour.Value; | |
var view = UnityEditor.SceneView.currentDrawingSceneView; | |
Vector3 screenPos = view.camera.WorldToScreenPoint(worldPos); | |
Vector2 size = GUI.skin.label.CalcSize(new GUIContent(text)); | |
GUI.Label(new Rect(screenPos.x - (size.x / 2), -screenPos.y + view.position.height + 4, size.x, size.y), text); | |
UnityEditor.Handles.EndGUI(); | |
} |
@nukadelic great solution! Simple! Thanks!
Thanks a lot, works great!
For those who wonder what this pixelRatio is about: it depends on the display UI scaling (e.g. 1.5 for the '150%' setting in windows)
Instead of computing it, you can actually just get it using
var pixelRatio = EditorGUIUtility.pixelsPerPoint;
(see https://docs.unity3d.com/ScriptReference/EditorGUIUtility-pixelsPerPoint.html)
Very neat, exactly what I was looking for!
I passed a color and it made the whole scene view that color. I just removed it, and it satisfied what I intended. Well done! Thanks
I solved the issue color issue and also fixed the scene zoom text slipping
public static void DrawString(string text, Vector3 worldPos, Color? colour = null)
{
UnityEditor.Handles.BeginGUI();
Color defaultColor = GUI.color;
if (colour.HasValue) GUI.color = colour.Value;
var view = UnityEditor.SceneView.currentDrawingSceneView;
Vector3 screenPos = view.camera.WorldToScreenPoint(worldPos);
Vector2 size = GUI.skin.label.CalcSize(new GUIContent(text));
GUI.Label(new Rect(screenPos.x - (size.x / 2), -screenPos.y + view.position.height - size.y * 2, size.x, size.y), text);
GUI.color = defaultColor;
UnityEditor.Handles.EndGUI();
}
Thanks all for the snippet and tweaks! Based on the GUI version above, I further improved by using
GUIStyle
to specify color and font size without having to use the color restore logic. I also added pixel-precise positioning with anchor support (e.g.Vector2.zero
= center,Vector2.one
= anchor to bottom-left corner,-Vector2.one
= anchor to top-right corner, etc.). The fudge factor issue is fixed by usingcamera.pixelHeight
instead ofview.position.height
.