Skip to content

Instantly share code, notes, and snippets.

@iDevelopThings
Created February 12, 2023 03:24
Show Gist options
  • Save iDevelopThings/b3009b8fc9a5205baef3e93d558de258 to your computer and use it in GitHub Desktop.
Save iDevelopThings/b3009b8fc9a5205baef3e93d558de258 to your computer and use it in GitHub Desktop.
GizmoTextPls
using UnityEditor;
using UnityEngine;
namespace Game.Util.Extensions
{
public class GizmoExtension
{
public static void DrawString(string text, Vector3 worldPosition, Color textColor, Vector2 anchor, float textSize = 15f)
{
#if UNITY_EDITOR
var view = SceneView.currentDrawingSceneView;
if (!view)
return;
var screenPosition = view.camera.WorldToScreenPoint(worldPosition);
if (screenPosition.y < 0 || screenPosition.y > view.camera.pixelHeight || screenPosition.x < 0 || screenPosition.x > view.camera.pixelWidth || screenPosition.z < 0)
return;
var pixelRatio = HandleUtility.GUIPointToScreenPixelCoordinate(Vector2.right).x - HandleUtility.GUIPointToScreenPixelCoordinate(Vector2.zero).x;
Handles.BeginGUI();
var style = new GUIStyle(GUI.skin.label) {
fontSize = (int) textSize,
normal = new GUIStyleState {textColor = textColor}
};
var size = style.CalcSize(new GUIContent(text)) * pixelRatio;
var alignedPosition =
((Vector2) screenPosition +
size * ((anchor + Vector2.left + Vector2.up) / 2f)) * (Vector2.right + Vector2.down) +
Vector2.up * view.camera.pixelHeight;
GUI.Label(new Rect(alignedPosition / pixelRatio, size / pixelRatio), text, style);
Handles.EndGUI();
#endif
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment