620 字
3 分钟
Unity 组件扩展工具集

自定义摄像机 Gizmo 扩展#

这个扩展为 Unity 摄像机组件添加了自定义的 Gizmo 显示功能,可以直观地展示摄像机的视口区域和关键参考线。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace PersonalTool
{
[CustomEditor(typeof(Camera))]
public class CameraCustomGizmos
{
private static float topBarHeight = 118.0328f;
private static Mesh quad = null;
private static float distance;
private static Vector3 buttomLeft;
private static Vector3 buttomRight;
private static Vector3 topLeft;
private static Vector3 topRight;
[DrawGizmo(GizmoType.Selected | GizmoType.Active)]
private static void OnDrawGizmosSelected(Camera camera, GizmoType gizmoType)
{
if (quad is null)
{
//Library/unity default resources::Cube
var type = typeof(Mesh);
quad = AssetDatabase.LoadAllAssetsAtPath("Library/unity default resources")
.Where(e => type.IsAssignableFrom(e.GetType()) && e.name == "Quad").Cast<Mesh>()?.First();
}
distance = camera.nearClipPlane * 1.001f;
buttomLeft = new Vector3(0f, 0f, distance);
buttomRight = new Vector3(Screen.width, 0f, distance);
topLeft = new Vector3(0f, Screen.height, distance);
topRight = new Vector3(Screen.width, Screen.height, distance);
buttomLeft = camera.ScreenToWorldPoint(buttomLeft);
buttomRight = camera.ScreenToWorldPoint(buttomRight);
topLeft = camera.ScreenToWorldPoint(topLeft);
topRight = camera.ScreenToWorldPoint(topRight);
DrawBorderZone(camera);
DrawViewCenterLine();
DrawCenterPoint(camera);
}
private static void DrawBorderZone(Camera camera)
{
var topBarButtomLeft = new Vector3(0f, Screen.height - topBarHeight, distance);
var topBarButtomRight = new Vector3(Screen.width, Screen.height - topBarHeight, distance);
topBarButtomLeft = camera.ScreenToWorldPoint(topBarButtomLeft);
topBarButtomRight = camera.ScreenToWorldPoint(topBarButtomRight);
#region TopBar
var width = Vector3.Distance(topBarButtomLeft, topBarButtomRight);
var height = Vector3.Distance(topBarButtomLeft, topLeft);
var size = new Vector3(width, height, 1f);
Gizmos.color = new Color32(255, 0, 0, 60);
Gizmos.DrawMesh(quad, (topBarButtomLeft + topRight) / 2f, camera.transform.rotation, size);
#endregion
var downBarTopLeft = new Vector3(0f, Screen.height * 0.1f, distance);
var downBarTopRight = new Vector3(Screen.width, Screen.height * 0.1f, distance);
downBarTopLeft = camera.ScreenToWorldPoint(downBarTopLeft);
downBarTopRight = camera.ScreenToWorldPoint(downBarTopRight);
#region ButtomBar
width = Vector3.Distance(buttomLeft, buttomRight);
height = Vector3.Distance(buttomLeft, downBarTopLeft);
size = new Vector3(width, height, 1f);
Gizmos.color = new Color32(255, 168, 0, 60);
Gizmos.DrawMesh(quad, (buttomLeft + downBarTopRight) / 2f, camera.transform.rotation, size);
#endregion
var leftBarButtomRight = new Vector3(Screen.width * 0.1f, Screen.height * 0.1f, distance);
var leftBarTopRight = new Vector3(Screen.width * 0.1f, Screen.height - topBarHeight, distance);
leftBarButtomRight = camera.ScreenToWorldPoint(leftBarButtomRight);
leftBarTopRight = camera.ScreenToWorldPoint(leftBarTopRight);
#region LeftBar
width = Vector3.Distance(downBarTopLeft, leftBarButtomRight);
height = Vector3.Distance(downBarTopLeft, topBarButtomLeft);
size = new Vector3(width, height, 1f);
Gizmos.color = new Color32(255, 255, 0, 60);
Gizmos.DrawMesh(quad, (downBarTopLeft + leftBarTopRight) / 2f, camera.transform.rotation, size);
#endregion
var rightBarButtomLeft = new Vector3(Screen.width * 0.9f, Screen.height * 0.1f, distance);
var rightBarTopLeft = new Vector3(Screen.width * 0.9f, Screen.height - topBarHeight, distance);
rightBarButtomLeft = camera.ScreenToWorldPoint(rightBarButtomLeft);
rightBarTopLeft = camera.ScreenToWorldPoint(rightBarTopLeft);
#region RightBar
width = Vector3.Distance(rightBarButtomLeft, downBarTopRight);
height = Vector3.Distance(rightBarButtomLeft, rightBarTopLeft);
size = new Vector3(width, height, 1f);
Gizmos.color = new Color32(255, 255, 0, 60);
Gizmos.DrawMesh(quad, (rightBarButtomLeft + topBarButtomRight) / 2f, camera.transform.rotation, size);
#endregion
}
private static void DrawViewCenterLine()
{
Gizmos.color = Color.cyan;
Gizmos.DrawLine((topLeft + buttomLeft) / 2f, (topRight + buttomRight) / 2f);
Gizmos.DrawLine((topLeft + topRight) / 2f, (buttomLeft + buttomRight) / 2f);
}
private static void DrawCenterPoint(Camera camera)
{
Gizmos.color = new Color32(255, 255, 0, 255);
var length = Vector3.Distance(buttomLeft, topRight) * 0.005f;
var size = new Vector3(length, length, 1f);
Gizmos.DrawMesh(quad, (buttomLeft + topRight) / 2f, camera.transform.rotation, size);
}
}
}

文本组件扩展#

这个扩展为 Unity 的 UI 文本组件添加了实用的编辑功能,包括空格替换和首行缩进功能。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace PersonalTool
{
[CustomEditor(typeof(Text))]
public class TextComponentExtension : UnityEditor.UI.TextEditor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.Space();
if (GUILayout.Button("替换所有空格为不换行空格"))
{
var sp = serializedObject.FindProperty("m_Text");
sp.stringValue = sp.stringValue.Replace(" ", "\u00A0");
serializedObject.ApplyModifiedProperties();
}
if (GUILayout.Button("添加首行缩进"))
{
var sp = serializedObject.FindProperty("m_Text");
if (!sp.stringValue.StartsWith("\u3000\u3000"))
sp.stringValue = sp.stringValue.Insert(0, "\u3000\u3000");
sp.stringValue = Regex.Replace(sp.stringValue, @"\n(?![\r\n\u3000])", "\n\u3000\u3000");
serializedObject.ApplyModifiedProperties();
}
}
}
}
Unity 组件扩展工具集
https://blog.unknowncat2048.top/posts/unity-component-extension-archive/
作者
碌碌无为喵神SAMA
发布于
2024-08-01
许可协议
CC BY-NC-SA 4.0