2025-12-01 16:46:28 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEditor.Overlays;
|
|
|
|
|
|
using UnityEditor.Toolbars;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
2025-12-09 20:31:44 +08:00
|
|
|
|
namespace AlicizaX.UXTool
|
2025-12-01 16:46:28 +08:00
|
|
|
|
{
|
|
|
|
|
|
[Overlay(typeof(SceneView), "Align")]
|
|
|
|
|
|
public class AlignToolbarOverlay : ToolbarOverlay
|
|
|
|
|
|
{
|
|
|
|
|
|
AlignToolbarOverlay() : base(
|
|
|
|
|
|
AlignTopLeft.id,
|
|
|
|
|
|
AlignTopCenter.id,
|
|
|
|
|
|
AlignTopRight.id,
|
|
|
|
|
|
AlignBottomLeft.id,
|
|
|
|
|
|
AlignBottomCenter.id,
|
|
|
|
|
|
AlignBottomRight.id,
|
|
|
|
|
|
AlignHorizontalLeft.id,
|
|
|
|
|
|
AlignHorizontalCenter.id,
|
|
|
|
|
|
AlignHorizontalRight.id
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 12 个按钮的初始化
|
|
|
|
|
|
[EditorToolbarElement(AlignTopLeft.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignTopLeft : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignTopLeft";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignTopLeft()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_vertically_top_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "顶部左对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.TopLeft);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignTopCenter.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignTopCenter : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignTopCenter";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignTopCenter()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_vertically_center_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "顶部居中对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.TopCenter);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignTopRight.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignTopRight : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignTopRight";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignTopRight()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_vertically_top_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "顶部右对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.TopRight);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignBottomLeft.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignBottomLeft : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignBottomLeft";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignBottomLeft()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_vertically_bottom_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "左下对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.BottomLeft);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignBottomCenter.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignBottomCenter : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignBottomCenter";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignBottomCenter()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_vertically_center_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "底部居中对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.BottomCenter);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignBottomRight.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignBottomRight : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignBottomRight";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignBottomRight()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_vertically_bottom_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "右下对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.BottomRight);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignHorizontalLeft.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignHorizontalLeft : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignHorizontalLeft";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignHorizontalLeft()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_horizontally_left_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "水平左对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.HorizontalLeft);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignHorizontalCenter.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignHorizontalCenter : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignHorizontalCenter";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignHorizontalCenter()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_horizontally_center_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "水平居中对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.HorizontalCenter);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[EditorToolbarElement(AlignHorizontalRight.id, typeof(SceneView))]
|
|
|
|
|
|
class AlignHorizontalRight : EditorToolbarButton
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string id = "AlignToolbarOverlay/AlignHorizontalRight";
|
|
|
|
|
|
|
|
|
|
|
|
public AlignHorizontalRight()
|
|
|
|
|
|
{
|
|
|
|
|
|
text = "";
|
|
|
|
|
|
icon = EditorGUIUtility.IconContent("d_align_horizontally_right_active").image as Texture2D; // 使用带 _active 后缀的图标
|
|
|
|
|
|
tooltip = "水平右对齐";
|
|
|
|
|
|
clicked += () => OnAlign(AlignType.HorizontalRight);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 枚举定义所有对齐类型
|
|
|
|
|
|
public enum AlignType
|
|
|
|
|
|
{
|
|
|
|
|
|
TopLeft,
|
|
|
|
|
|
TopCenter,
|
|
|
|
|
|
TopRight,
|
|
|
|
|
|
BottomLeft,
|
|
|
|
|
|
BottomCenter,
|
|
|
|
|
|
BottomRight,
|
|
|
|
|
|
HorizontalLeft,
|
|
|
|
|
|
HorizontalCenter,
|
|
|
|
|
|
HorizontalRight
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 对齐方法
|
|
|
|
|
|
private static void OnAlign(AlignType alignType)
|
|
|
|
|
|
{
|
|
|
|
|
|
GameObject selectedObject = Selection.activeGameObject;
|
|
|
|
|
|
|
|
|
|
|
|
if (selectedObject == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
RectTransform rectTransform = selectedObject.GetComponent<RectTransform>();
|
|
|
|
|
|
if (rectTransform == null) return;
|
|
|
|
|
|
Undo.RecordObject(rectTransform, alignType.ToString());
|
|
|
|
|
|
// 简化对齐逻辑,设置锚点和位置
|
|
|
|
|
|
switch (alignType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case AlignType.TopLeft:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(0, 1);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(0, 1);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.TopCenter:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(0.5f, 1);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(0.5f, 1);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.TopRight:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(1, 1);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(1, 1);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.BottomLeft:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(0, 0);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(0, 0);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.BottomCenter:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(0.5f, 0);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(0.5f, 0);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.BottomRight:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(1, 0);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(1, 0);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.HorizontalLeft:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(0, 0.5f);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(0, 0.5f);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.HorizontalCenter:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case AlignType.HorizontalRight:
|
|
|
|
|
|
rectTransform.anchorMin = new Vector2(1, 0.5f);
|
|
|
|
|
|
rectTransform.anchorMax = new Vector2(1, 0.5f);
|
|
|
|
|
|
rectTransform.anchoredPosition = Vector2.zero;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|