You've already forked CC-Framework.EditorExtend
init
This commit is contained in:
15
Assets/CCTools/Editor/CCTools.RectTransform.Editor.asmdef
Normal file
15
Assets/CCTools/Editor/CCTools.RectTransform.Editor.asmdef
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "com.foldcc.cc-framework.editorextend.rectransform",
|
||||
"references": [],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce44b0a50011c574f9e9d48cbe303171
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/CCTools/Editor/Config.meta
Normal file
8
Assets/CCTools/Editor/Config.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa705cc16ba117d41b40db06dc01e4ce
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
88
Assets/CCTools/Editor/Config/CCToolsConfig.cs
Normal file
88
Assets/CCTools/Editor/Config/CCToolsConfig.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
|
||||
|
||||
namespace CCTools.Config
|
||||
|
||||
{
|
||||
|
||||
public static class CCToolsConfig
|
||||
|
||||
{
|
||||
|
||||
private const string ENABLE_FIGMA_UPDATE_KEY = "CCTools.EnableFigmaUpdate";
|
||||
|
||||
private const string MENU_ITEM_PATH = "CC-Tools/RectTransform Tool/Enable Figma Update";
|
||||
|
||||
|
||||
|
||||
// 默认启用
|
||||
public static bool enableFigmaUpdate = true;
|
||||
|
||||
|
||||
|
||||
static CCToolsConfig()
|
||||
{
|
||||
|
||||
// 从 EditorPrefs 加载保存的状态
|
||||
enableFigmaUpdate = EditorPrefs.GetBool(ENABLE_FIGMA_UPDATE_KEY, true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static bool EnableFigmaUpdate
|
||||
|
||||
{
|
||||
|
||||
get => enableFigmaUpdate;
|
||||
|
||||
private set
|
||||
{
|
||||
|
||||
if (enableFigmaUpdate != value)
|
||||
|
||||
{
|
||||
|
||||
enableFigmaUpdate = value;
|
||||
|
||||
EditorPrefs.SetBool(ENABLE_FIGMA_UPDATE_KEY, value);
|
||||
|
||||
// 刷新编辑器
|
||||
UnityEditorInternal.InternalEditorUtility.RepaintAllViews();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MenuItem(MENU_ITEM_PATH)]
|
||||
|
||||
private static void ToggleFigmaUpdate()
|
||||
|
||||
{
|
||||
|
||||
enableFigmaUpdate = !enableFigmaUpdate;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[MenuItem(MENU_ITEM_PATH, true)]
|
||||
|
||||
private static bool ToggleFigmaUpdateValidate()
|
||||
|
||||
{
|
||||
|
||||
Menu.SetChecked(MENU_ITEM_PATH, enableFigmaUpdate);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/CCTools/Editor/Config/CCToolsConfig.cs.meta
Normal file
11
Assets/CCTools/Editor/Config/CCToolsConfig.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 07c448ff099a133439867c8cd9f41938
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/CCTools/Editor/ExtendedRectTransformEditor.cs.meta
Normal file
11
Assets/CCTools/Editor/ExtendedRectTransformEditor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ffd275f366fb314f93ccabdd61630fd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user