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