码迷,mamicode.com
首页 > 其他好文 > 详细

复制、粘贴一个物体的所有组件

时间:2016-05-22 23:01:11      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

复制、粘贴一个物体的所有组件

标签:

原文地址:http://www.cnblogs.com/bzyzhang/p/5517862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!