Unity Editor 深拷贝 组件

来源:互联网 发布:修改表数据的sql语句 编辑:程序博客网 时间:2024/06/08 19:10

Unity Editor 深拷贝 组件

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

这里写图片描述

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<Component>();    }    [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);            }        }    }}