mirror of
https://github.com/DCFApixels/Unity-DebugX.git
synced 2025-09-17 17:34:35 +08:00
update text/fix size bug/fix code style
This commit is contained in:
parent
9b9a52552f
commit
c0a2f4e744
8
Runtime/Gizmos/Text.meta
Normal file
8
Runtime/Gizmos/Text.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9709faba0386b5244a8ab4c9c7ce49ec
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,17 +1,34 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DCFApixels {
|
||||
using IN = MethodImplAttribute;
|
||||
namespace DCFApixels
|
||||
{
|
||||
using DrawHandler = DebugX.DrawHandler;
|
||||
using IN = MethodImplAttribute;
|
||||
|
||||
public static class TextDrawHandlerExtensions {
|
||||
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) =>
|
||||
drawHandler.Gizmo(new TextGizmo(position, text, settings));
|
||||
#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));
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
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);
|
||||
GUI.color = item.Color * DebugX.GlobalColor;
|
||||
}
|
||||
Color color= item.Color * DebugX.GlobalColor;
|
||||
if (fontSize < 1)
|
||||
{
|
||||
color.a *= fontSize;
|
||||
}
|
||||
GUI.color = color;
|
||||
style.Draw(rect, _labelDummy, false, false, false, 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
|
||||
|
Loading…
Reference in New Issue
Block a user