Unity编辑器扩展:UI绑定复制工具

在UI开发中,当我们需要为相似预制体快速复制字段绑定时,手动操作既繁琐又容易出错。本文将介绍一个高效解决这一痛点的Unity编辑器扩展工具。(适用于换皮活动)

这是一个UI环比开发的绑定复制工具,主要功能是将公共字段和[SerializeField]标记的引用字段从一个MonoBehaviour组件复制到另一个组件。它特别适用于以下场景:

  1. 替换UI界面风格时复用绑定逻辑
  2. 在相似组件之间快速转移引用
  3. 避免重复拖拽引用字段的操作

使用方式

  1. 通过 Tools > UI Binding Copier 打开窗口
  2. 拖拽源组件(已配置好绑定的组件)
  3. 拖拽目标组件(需要接收绑定的组件)
  4. 点击 Copy Bindings 按钮完成复制
csharp 复制代码
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

public class BindingCopier : EditorWindow
{
    private MonoBehaviour sourceComponent;
    private MonoBehaviour targetComponent;

    [MenuItem("Tools/UI Binding Copier")]
    public static void ShowWindow()
    {
        GetWindow<BindingCopier>("Binding Copier");
    }

    void OnGUI()
    {
        GUILayout.Label("Copy UI Bindings Between Components", EditorStyles.boldLabel);

        sourceComponent = (MonoBehaviour)EditorGUILayout.ObjectField(
            "Source Component",
            sourceComponent,
            typeof(MonoBehaviour),
            true);

        targetComponent = (MonoBehaviour)EditorGUILayout.ObjectField(
            "Target Component",
            targetComponent,
            typeof(MonoBehaviour),
            true);

        if (GUILayout.Button("Copy Bindings"))
        {
            if (sourceComponent != null && targetComponent != null)
            {
                CopyBindings(sourceComponent, targetComponent);
                EditorUtility.DisplayDialog("Success", "Bindings copied successfully!", "OK");
            }
            else
            {
                EditorUtility.DisplayDialog("Error", "Please assign both source and target components", "OK");
            }
        }

        EditorGUILayout.HelpBox(
            "这个工具可以把公共字段和[SerializeField]引用从一个组件复制到另一个组件" +
            "当组件具有相似的字段名称和类型时,效果最佳(C#换皮用)",
            MessageType.Info);
    }

    public static void CopyBindings(MonoBehaviour source, MonoBehaviour target)
    {
        if (source == null || target == null) return;

        SerializedObject sourceSerialized = new SerializedObject(source);
        SerializedObject targetSerialized = new SerializedObject(target);

        SerializedProperty iterator = sourceSerialized.GetIterator();
        bool enterChildren = true;

        while (iterator.NextVisible(enterChildren))
        {
            enterChildren = false;

            if (iterator.propertyType == SerializedPropertyType.ObjectReference)
            {
                string propertyName = iterator.name;
                SerializedProperty targetProperty = targetSerialized.FindProperty(propertyName);

                if (targetProperty != null &&
                    targetProperty.propertyType == SerializedPropertyType.ObjectReference &&
                    targetProperty.objectReferenceValue == null) // Optional: only overwrite null values
                {
                    targetSerialized.CopyFromSerializedProperty(iterator);
                }
            }
        }

        targetSerialized.ApplyModifiedProperties();
        EditorUtility.SetDirty(target);
    }
}
#endif

效果如下所示

只需要将预制体上的组件拖动过去 就能快速的复用绑定关系

适用场景:适用于换皮活动C#逻辑大致相似但需要单独提出逻辑

相关推荐
马剑威(威哥爱编程)1 小时前
鸿蒙6开发中,UI相关应用崩溃常见问题与解决方案
ui·华为·harmonyos
ITVV4 小时前
元数据 Unity Catalog v0.3.0 UI
ui·元数据
霜绛6 小时前
Unity:lua热更新(三)——Lua语法(续)
unity·游戏引擎·lua
世洋Blog14 小时前
更好的利用ChatGPT进行项目的开发
人工智能·unity·chatgpt
ganshenml19 小时前
sed 流编辑器在前端部署中的作用
前端·编辑器
evolution_language1 天前
Unity场景(Scene)的注意事项和易错点
unity·游戏引擎·scene
sulikey1 天前
从入门到精通:如何自己编写高质量的 .gitignore(面向工程实践)
git·gitee·编辑器·gitlab·github·gitignore·gitattributes
袅沫1 天前
Element-UI 番外表格组件
javascript·vue.js·ui
EQ-雪梨蛋花汤1 天前
【AI工具】使用 Doubao-Seed-Code 优化 Unity 编辑器插件:从功能实现到界面美化的完整实践
人工智能·unity·编辑器
Dr.勿忘1 天前
开源Unity小框架:高效单例与模块化设计
游戏·unity·开源·c#·游戏引擎·游戏程序·gamejam