You've already forked CC-Framework.EditorExtend
init
This commit is contained in:
127
Assets/CCTools/Editor/ExtendedRectTransformEditor.cs
Normal file
127
Assets/CCTools/Editor/ExtendedRectTransformEditor.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using CCTools.Config;
|
||||
|
||||
[CustomEditor(typeof(RectTransform))]
|
||||
public class ExtendedRectTransformEditor : Editor
|
||||
{
|
||||
private RectTransform rectTransform;
|
||||
private Editor defaultEditor;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
rectTransform = target as RectTransform;
|
||||
defaultEditor = CreateEditor(target, typeof(Editor).Assembly.GetType("UnityEditor.RectTransformEditor"));
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (defaultEditor != null)
|
||||
{
|
||||
DestroyImmediate(defaultEditor);
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 GetFigmaPosition()
|
||||
{
|
||||
Vector2 parentSize = Vector2.zero;
|
||||
if (rectTransform.parent is RectTransform parentRect)
|
||||
{
|
||||
parentSize = parentRect.rect.size;
|
||||
}
|
||||
|
||||
float figmaX = rectTransform.anchoredPosition.x +
|
||||
(parentSize.x * rectTransform.anchorMin.x) -
|
||||
(rectTransform.rect.width * rectTransform.pivot.x);
|
||||
|
||||
float figmaY = -rectTransform.anchoredPosition.y +
|
||||
(parentSize.y * (1 - rectTransform.anchorMax.y)) -
|
||||
(rectTransform.rect.height * (1 - rectTransform.pivot.y));
|
||||
|
||||
return new Vector2(figmaX, figmaY);
|
||||
}
|
||||
|
||||
private void SetFigmaPosition(Vector2 figmaPos)
|
||||
{
|
||||
Vector2 parentSize = Vector2.zero;
|
||||
if (rectTransform.parent is RectTransform parentRect)
|
||||
{
|
||||
parentSize = parentRect.rect.size;
|
||||
}
|
||||
|
||||
float anchoredX = figmaPos.x -
|
||||
(parentSize.x * rectTransform.anchorMin.x) +
|
||||
(rectTransform.rect.width * rectTransform.pivot.x);
|
||||
|
||||
float anchoredY = -(figmaPos.y -
|
||||
(parentSize.y * (1 - rectTransform.anchorMax.y)) +
|
||||
(rectTransform.rect.height * (1 - rectTransform.pivot.y)));
|
||||
|
||||
rectTransform.anchoredPosition = new Vector2(anchoredX, anchoredY);
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
// 绘制默认Inspector
|
||||
if (defaultEditor != null)
|
||||
{
|
||||
defaultEditor.OnInspectorGUI();
|
||||
}
|
||||
|
||||
// 如果功能被禁用,直接返回
|
||||
if (!CCToolsConfig.EnableFigmaUpdate)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
// 开始Figma区域
|
||||
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
|
||||
|
||||
EditorGUILayout.Space(5);
|
||||
EditorGUILayout.LabelField("坐标同步器 (Figma、MasterGo)", EditorStyles.boldLabel);
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
Vector2 figmaPos = GetFigmaPosition();
|
||||
|
||||
EditorGUI.indentLevel++;
|
||||
|
||||
// 位置输入框
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUIUtility.labelWidth = 60;
|
||||
float newFigmaX = EditorGUILayout.FloatField("X 坐标", figmaPos.x);
|
||||
float newFigmaY = EditorGUILayout.FloatField("Y 坐标", figmaPos.y);
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
// 尺寸输入框
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUIUtility.labelWidth = 60;
|
||||
float newWidth = EditorGUILayout.FloatField("宽度", rectTransform.rect.width);
|
||||
float newHeight = EditorGUILayout.FloatField("高度", rectTransform.rect.height);
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
EditorGUI.indentLevel--;
|
||||
|
||||
EditorGUILayout.Space(5);
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
Undo.RecordObject(rectTransform, "修改 Figma 坐标和尺寸");
|
||||
|
||||
// 更新位置
|
||||
SetFigmaPosition(new Vector2(newFigmaX, newFigmaY));
|
||||
|
||||
// 更新尺寸
|
||||
rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
|
||||
|
||||
// 保持左上角位置不变
|
||||
SetFigmaPosition(new Vector2(newFigmaX, newFigmaY));
|
||||
|
||||
EditorUtility.SetDirty(rectTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user