unity复制所有组件的值&&在hierarchy面板添加按钮
?
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
public class CopyAllComponent : EditorWindow
{
static Component[] copiedComponents;
[MenuItem("GameObject/复制所有组件", false, 0)]
[MenuItem("Component Editor/Copy Component")]
static void DoCopyComponent()
{
copiedComponents = Selection.activeGameObject.GetComponents<Component>();
}
[MenuItem("GameObject/粘贴所有组件Value", false, 0)]
[MenuItem("Component Editor/Paste Component")]
static void DoPasteComponent()
{
if (copiedComponents == null)
{
return;
}
GameObject targetObject = Selection.activeGameObject;
if (targetObject == null)
{
return;
}
for (int i = 0; i < copiedComponents.Length; i++)
{
Component newComponent = copiedComponents[i];
if (newComponent == null) continue;
UnityEditorInternal.ComponentUtility.CopyComponent(newComponent);
Component oldComponent = targetObject.GetComponent(newComponent.GetType());
if (oldComponent != null)//如果当前选择对象有此组件,只粘贴组件的值
{
UnityEditorInternal.ComponentUtility.PasteComponentValues(oldComponent);
}
else//如果当前选择对象没有此组件,直接粘贴组件
{
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetObject);
}
}
}
}
效果
?
?
|