复制、粘贴一个物体的所有组件
1 using UnityEngine; 2 using UnityEditor; 3 using System.Collections.Generic; 4 5 public class CopyAllComponent : EditorWindow 6 { 7 static Component[] copiedComponents; 8 [MenuItem("GameObject/Copy Current Components",false,10)] 9 static void Copy() 10 { 11 copiedComponents = Selection.activeGameObject.GetComponents<Component>(); 12 } 13 14 [MenuItem("GameObject/Paste Current Components",false,10)] 15 static void Paste() 16 { 17 foreach (var targetGameObject in Selection.gameObjects) 18 { 19 if (!targetGameObject || copiedComponents == null) continue; 20 21 var targetComponents = new List<Component>(targetGameObject.GetComponents<Component>()); 22 23 foreach (var copiedComponent in copiedComponents) 24 { 25 if (!copiedComponent) continue; 26 27 var targetComponent = targetComponents.Find((item) => item.GetType() == copiedComponent.GetType()); 28 29 UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent); 30 31 if (targetComponent != null) 32 { 33 UnityEditorInternal.ComponentUtility.PasteComponentValues(targetComponent); 34 } 35 else 36 { 37 UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject); 38 } 39 } 40 } 41 } 42 43 }
随意耕种,只为自己!