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

View File

@ -1,17 +1,34 @@
using System.Runtime.CompilerServices;
using UnityEngine;
namespace DCFApixels {
using IN = MethodImplAttribute;
namespace DCFApixels
{
using DrawHandler = DebugX.DrawHandler;
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));
using IN = MethodImplAttribute;
[IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text, DebugXTextSettings settings) =>
drawHandler.Gizmo(new TextGizmo(position, text, settings));
public static class TextDrawHandlerExtensions
{
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.Rendering;
namespace DCFApixels {
namespace DCFApixels
{
using IN = MethodImplAttribute;
internal readonly struct TextGizmo : IGizmo<TextGizmo> {
private const MethodImplOptions LINE = DebugX.LINE;
internal readonly struct TextGizmo : IGizmo<TextGizmo>
{
private const MethodImplOptions LINE = MethodImplOptions.AggressiveInlining;
public readonly Vector3 Position;
public readonly string Text;
public readonly DebugXTextSettings Settings;
@ -64,16 +65,28 @@ namespace DCFApixels {
GUIStyle style = _labelStyle;
var zoom = GetCameraZoom(camera, item.Value.Position);
style.fontSize = Mathf.FloorToInt(Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor));
float fontSize = 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;
if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f))
{
Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle);
Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor;
Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1);
GUI.color = item.Color * DebugX.GlobalColor;
if (item.Value.Settings.IsHasBackground)
{
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);
}
}
@ -104,10 +117,10 @@ namespace DCFApixels {
result.background = null;
return result;
}
// 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
_labelStyle = new GUIStyle(skin.label)
{
richText = false,
@ -190,12 +203,12 @@ namespace DCFApixels {
private static float GetCameraZoom(Camera camera, Vector3 position)
{
position = Handles.matrix.MultiplyPoint(position);
Transform transform = camera.transform;
Vector3 position2 = transform.position;
float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f)));
Vector3 vector = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z)));
Vector3 vector2 = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z)));
float magnitude = (vector - vector2).magnitude;
Transform cameraTransform = camera.transform;
Vector3 cameraPos = cameraTransform.position;
float z = Vector3.Dot(position - cameraPos, cameraTransform.TransformDirection(new Vector3(0f, 0f, 1f)));
Vector3 pos1 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(0f, 0f, z)));
Vector3 pos2 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(1f, 0f, z)));
float magnitude = (pos1 - pos2).magnitude;
return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint;
}
#endregion