update text/fix size bug/fix code style

This commit is contained in:
DCFApixels 2025-03-05 18:50:22 +08:00
parent 9b9a52552f
commit c0a2f4e744
4 changed files with 74 additions and 36 deletions

8
Runtime/Gizmos/Text.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9709faba0386b5244a8ab4c9c7ce49ec
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -12,7 +12,7 @@ namespace DCFApixels
public const float SCREEN_SPACE_SCALE_FACTOR = 0f; public const float SCREEN_SPACE_SCALE_FACTOR = 0f;
public const float WORLD_SPACE_SCALE_FACTOR = 1f; public const float WORLD_SPACE_SCALE_FACTOR = 1f;
public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0); public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0);
public static readonly DebugXTextSettings WorldSpaceScale = Default.SetWorldSpaceScaleFactor(); public static readonly DebugXTextSettings WorldSpaceScale = Default.SetWorldSpaceScaleFactor();
@ -20,15 +20,15 @@ namespace DCFApixels
/// Font size. Default is <see cref="DEFAULT_FONT_SIZE" />. /// Font size. Default is <see cref="DEFAULT_FONT_SIZE" />.
/// </summary> /// </summary>
public readonly int FontSize; public readonly int FontSize;
/// <summary> /// <summary>
/// Text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />. /// Text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />.
/// </summary> /// </summary>
public readonly TextAnchor TextAnchor; public readonly TextAnchor TextAnchor;
public readonly Color BackgroundColor; public readonly Color BackgroundColor;
public readonly float WorldSpaceScaleFactor; public readonly float WorldSpaceScaleFactor;
// ReSharper disable once UnusedMember.Global // ReSharper disable once UnusedMember.Global
public bool IsHasBackground => BackgroundColor.a > 0; public bool IsHasBackground => BackgroundColor.a > 0;
@ -47,7 +47,7 @@ namespace DCFApixels
{ {
return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceScaleFactor); return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceScaleFactor);
} }
/// <summary> /// <summary>
/// Sets text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />. /// Sets text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />.
/// </summary> /// </summary>
@ -55,7 +55,7 @@ namespace DCFApixels
{ {
return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceScaleFactor); return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceScaleFactor);
} }
/// <summary> /// <summary>
/// Sets background image color behind text. Ignored if transparent. /// Sets background image color behind text. Ignored if transparent.
/// </summary> /// </summary>
@ -63,7 +63,7 @@ namespace DCFApixels
{ {
return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceScaleFactor); return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceScaleFactor);
} }
/// <summary> /// <summary>
/// Synchronizes the text scale in screen space. The text will remain the same size on the screen. /// Synchronizes the text scale in screen space. The text will remain the same size on the screen.
/// </summary> /// </summary>
@ -72,7 +72,7 @@ namespace DCFApixels
{ {
return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_FACTOR); return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_FACTOR);
} }
/// <summary> /// <summary>
/// Synchronizes the text scale in world space. The text will remain the same size on the scene. /// Synchronizes the text scale in world space. The text will remain the same size on the scene.
/// </summary> /// </summary>
@ -80,7 +80,7 @@ namespace DCFApixels
{ {
return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_FACTOR); return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_FACTOR);
} }
/// <summary> /// <summary>
/// Allows you to control the text scale depending on the camera zoom. /// Allows you to control the text scale depending on the camera zoom.
/// </summary> /// </summary>

View File

@ -1,17 +1,34 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
namespace DCFApixels { namespace DCFApixels
using IN = MethodImplAttribute; {
using DrawHandler = DebugX.DrawHandler; using DrawHandler = DebugX.DrawHandler;
using IN = MethodImplAttribute;
public static class TextDrawHandlerExtensions {
private const MethodImplOptions LINE = DebugX.LINE;
[IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text) =>
drawHandler.Gizmo(new TextGizmo(position, text, DebugXTextSettings.Default));
[IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text, DebugXTextSettings settings) => public static class TextDrawHandlerExtensions
drawHandler.Gizmo(new TextGizmo(position, text, settings)); {
private const MethodImplOptions LINE = DebugX.LINE;
#if DEBUG
private static bool _singleWarningToggle = true;
#endif
[IN(LINE)]
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.Default);
[IN(LINE)]
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text, DebugXTextSettings settings)
{
if(settings.FontSize <= float.Epsilon)
{
#if DEBUG
if (_singleWarningToggle)
{
Debug.LogWarning("Text rendering requires FontSize > 0, otherwise the text will be invisible. To avoid invalid parameters, use DebugXTextSettings.Default instead of manual instantiation.");
_singleWarningToggle = false;
}
#endif
settings = settings.SetSize(DebugXTextSettings.DEFAULT_FONT_SIZE);
}
return h.Gizmo(new TextGizmo(position, text, settings));
}
} }
} }

View File

@ -4,11 +4,12 @@ using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering; using UnityEngine.Rendering;
namespace DCFApixels { namespace DCFApixels
{
using IN = MethodImplAttribute; using IN = MethodImplAttribute;
internal readonly struct TextGizmo : IGizmo<TextGizmo>
internal readonly struct TextGizmo : IGizmo<TextGizmo> { {
private const MethodImplOptions LINE = DebugX.LINE; private const MethodImplOptions LINE = MethodImplOptions.AggressiveInlining;
public readonly Vector3 Position; public readonly Vector3 Position;
public readonly string Text; public readonly string Text;
public readonly DebugXTextSettings Settings; public readonly DebugXTextSettings Settings;
@ -64,16 +65,28 @@ namespace DCFApixels {
GUIStyle style = _labelStyle; GUIStyle style = _labelStyle;
var zoom = GetCameraZoom(camera, item.Value.Position); var zoom = GetCameraZoom(camera, item.Value.Position);
float fontSize = Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor);
style.fontSize = Mathf.FloorToInt(Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor)); style.fontSize = Mathf.Max(1, Mathf.FloorToInt(fontSize));
style.alignment = item.Value.Settings.TextAnchor; style.alignment = item.Value.Settings.TextAnchor;
if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f)) if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f))
{ {
Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle);
Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor; if (item.Value.Settings.IsHasBackground)
Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1); {
GUI.color = item.Color * DebugX.GlobalColor; Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor;
if(fontSize < 1)
{
backgroundColor.a *= fontSize;
}
Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1);
}
Color color= item.Color * DebugX.GlobalColor;
if (fontSize < 1)
{
color.a *= fontSize;
}
GUI.color = color;
style.Draw(rect, _labelDummy, false, false, false, false); style.Draw(rect, _labelDummy, false, false, false, false);
} }
} }
@ -104,10 +117,10 @@ namespace DCFApixels {
result.background = null; result.background = null;
return result; return result;
} }
// If calling GUI.skin directly - Unity will throw exceptions saying that GUI.skin can be called only from OnSceneGUI context. // If calling GUI.skin directly - Unity will throw exceptions saying that GUI.skin can be called only from OnSceneGUI context.
GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin
_labelStyle = new GUIStyle(skin.label) _labelStyle = new GUIStyle(skin.label)
{ {
richText = false, richText = false,
@ -190,12 +203,12 @@ namespace DCFApixels {
private static float GetCameraZoom(Camera camera, Vector3 position) private static float GetCameraZoom(Camera camera, Vector3 position)
{ {
position = Handles.matrix.MultiplyPoint(position); position = Handles.matrix.MultiplyPoint(position);
Transform transform = camera.transform; Transform cameraTransform = camera.transform;
Vector3 position2 = transform.position; Vector3 cameraPos = cameraTransform.position;
float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f))); float z = Vector3.Dot(position - cameraPos, cameraTransform.TransformDirection(new Vector3(0f, 0f, 1f)));
Vector3 vector = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z))); Vector3 pos1 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(0f, 0f, z)));
Vector3 vector2 = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z))); Vector3 pos2 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(1f, 0f, z)));
float magnitude = (vector - vector2).magnitude; float magnitude = (pos1 - pos2).magnitude;
return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint; return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint;
} }
#endregion #endregion