This commit is contained in:
2025-02-11 17:05:39 +08:00
commit 72a217c3c8
15 changed files with 367 additions and 0 deletions

10
CHANGELOG.md Normal file
View File

@@ -0,0 +1,10 @@
# Changelog
All notable changes to this package will be documented in this file.
## [1.0.0] - 2024-03-XX
### Added
- 初始版本发布
- 支持Figma坐标系转换
- 添加全局开关功能
- 实时同步RectTransform属性

7
CHANGELOG.md.meta Normal file
View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 5ccc2ddb42ab9254ea4b92b66d686786
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Editor.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5a1f2125f2364c040aadaedc8a7d0607
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ce44b0a50011c574f9e9d48cbe303171
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Editor/Config.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: fa705cc16ba117d41b40db06dc01e4ce
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 07c448ff099a133439867c8cd9f41938
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7ffd275f366fb314f93ccabdd61630fd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

35
README.md Normal file
View File

@@ -0,0 +1,35 @@
# CC-Tools RectTransform
Unity编辑器扩展为RectTransform组件添加Figma坐标支持。
## 功能特性
- 在RectTransform组件中直接显示和编辑Figma坐标\MasterGo坐标
- 支持实时坐标系转换
- 保持UI元素的原有属性pivot、anchor等
- 可通过菜单快速开启/关闭功能
## 安装方法
### 通过 Unity Package Manager
1. 打开 Package Manager 窗口
2. 点击左上角的 "+" 按钮
3. 选择 "Add package from git URL"
4. 输入: `https://github.com/your-username/cc-tools-rectransform.git`
### 手动安装
下载并导入 .unitypackage 文件到您的项目中。
## 使用方法
1. 选择任意带有RectTransform组件的UI元素
2. 在Inspector窗口中查看扩展的Figma坐标区域
3. 直接输入Figma坐标值UI元素将自动更新位置
可以通过菜单 "CC-Tools/RectTransform Tool/Enable Figma Update" 来启用或禁用该功能。
## 要求
- Unity 2019.4 或更高版本

7
README.md.meta Normal file
View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 14b5a71194a901444aa95dd694ea4898
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Runtime.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: {生成的GUID}
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

18
package.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "com.foldcc.cc-framework.editorextend",
"version": "1.0.0",
"displayName": "CC-Tools RectTransform",
"description": "A Unity editor extension that adds Figma coordinate support to RectTransform components.",
"unity": "2019.4",
"keywords": [
"editor",
"ui",
"tools",
"figma"
],
"author": {
"name": "CC-Tools",
"email": "foldcc@lightyears.com",
"url": "https://lightyears.ltd"
}
}

7
package.json.meta Normal file
View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: de1ef936b31d2f348ad672db29997427
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: