Unity Editor 深拷贝 组件

Unity Editor 深拷贝 组件

GameObject Inspector 面板上 Copy Component 功能 的实现

Unity Editor 深拷贝 组件_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public class Copy : EditorWindow {

    static Component[] componentArr;
    [MenuItem("GameObject/Copy Component %#C")]
    static void CopyComponent()
    {
        componentArr = Selection.activeGameObject.GetComponents();
    }

    [MenuItem("GameObject/Paste Component %#V")]
    static void PasteComponent()
    {
        if (componentArr == null)
        {
            return;
        }

        GameObject targetGameObject = Selection.activeGameObject;
        if (!targetGameObject)
        {
            return;
        }

        for (int i = 0; i < componentArr.Length; ++i)
        {
            Component component = componentArr[i];
            if (!component)
            {
                continue;
            }

            UnityEditorInternal.ComponentUtility.CopyComponent(component);

            Type type = component.GetType();
            Component componentOld = targetGameObject.GetComponent(type);

            if (!componentOld)
            {
                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);
            }
            else
            {
                UnityEditorInternal.ComponentUtility.PasteComponentValues(component);
            }
        }
    }

}

你可能感兴趣的:(Unity之Editor)