打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Unity对象的所有组件深拷贝与粘贴

本文章由cartzhang编写,转载请注明出处。 所有权利保留。
文章链接:http://blog.csdn.net/cartzhang/article/details/51454847
作者:cartzhang

一、Unity 单个组件拷贝

Unity中,经常有对象或预制体,但是想要把某个预制体或对象的组件全部都拷贝到某个新的对象上。
Unity 虽然已经提供了Copy Component这个功能,


这个功能很不错,可以拷贝粘贴,但是面对某个组件上大量的组件和里面的各种参数调整,

对象或预制体的层级结构深,各层的组件多,参数也纷繁复杂,这个就不太灵光了。
*
怎么办呢?
网络上查出了, asset store上面有这个东西,所可以拷贝的。但是居然要求要5刀啊!!
哎呀,不科学啊!!网址就不给出了,因为有我,他们就不再被需要了。
咋办?

二、当前层所有组件的拷贝与粘贴

道理很简单,不就是复制和粘贴么?
哈哈!!先来简单的,两步走。
1.拷贝

  static Component[] copiedComponents;    [MenuItem("GameObject/Copy Current Components #&C")]    static void Copy()    {        copiedComponents = Selection.activeGameObject.GetComponents<Component>();    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

一句话,就是获取当前选中的对象的组件,然后存储于变量中。
2.粘贴

  [MenuItem("GameObject/Paste Current Components #&P")]    static void Paste()    {        foreach (var targetGameObject in Selection.gameObjects)        {            if (!targetGameObject || copiedComponents == null) continue;            foreach (var copiedComponent in copiedComponents)            {                if (!copiedComponent) continue;                UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14


这里面干了点啥?基本上都是自解释的句子。使用了UnityEditorInternal的ComponentUtility的函数。

CopyComponentPasteComponentAsNew
  • 1
  • 2
  • 1
  • 2

然后在编辑器的GameObject下就可以看到Copy Current Components和Paste Current Components两个菜单了,当然还包括他们的快捷键。
由于担心与其他快捷键重复,所以Ctrl,Shift,Alt都是用上了。当然你可以根据自己喜好来决定。

三、对象组件的深度复制与粘贴

上面的当前层的拷贝,已经把当前拷贝一个层的问题解决了。
深度拷贝,当然要深入到对象或预制体的所有子对象进行拷贝了。
1.
建立了一个链表来解决层级的问题。

public class MyComponentList    {        public MyComponentList()        {        }        public List<Component> gameObjList;        public List<MyComponentList> nextList;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9


2.拷贝对象组件,包括所有子对象组件内容

    static MyComponentList pri_my_list = new MyComponentList();    private static void GetAllChilds(GameObject transformForSearch, MyComponentList next)    {        List<Component> childsOfGameobject = new List<Component>();        next.gameObjList = childsOfGameobject;        next.nextList = new List<MyComponentList>();        foreach (var item in transformForSearch.GetComponents<Component>())        {            childsOfGameobject.Add(item);        }        foreach (Transform item in transformForSearch.transform)        {            MyComponentList tmpnext = new MyComponentList();            GetAllChilds(item.gameObject, tmpnext);            next.nextList.Add(tmpnext);        }        return;    }   [MenuItem("GameObject/Copy All Components #%&C")]    static void Copy()    {        GetAllChilds(Selection.activeGameObject,pri_my_list);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

3.选择新对象粘贴拷贝内容

    private static void PasteChildComponent(GameObject gameObj, MyComponentList next)    {        if (next.gameObjList != null)        {            foreach (var copiedComponent in next.gameObjList)            {                if (!copiedComponent) continue;                UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gameObj);            }        }        if (next.nextList != null)        {            List<Transform> TmpListTrans = new List<Transform>();            foreach (Transform item in gameObj.transform)            {                TmpListTrans.Add(item);            }            int i = 0;            foreach (var item in next.nextList)            {                if (i < TmpListTrans.Count)                {                    PasteChildComponent(TmpListTrans[i].gameObject, item);                }                i++;            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

稍微罗嗦一下,深度拷贝与粘贴组件都使用了递归调用。
粘贴的递归过程中,首先粘贴了当前层级的所有组件,方法仍旧使用的是第一种拷贝粘贴的方法。
然后遍历子对象中的对象,递归调用。
就是这样。

四、运行截图

五、当前层组件拷贝与粘贴源码

using UnityEngine;using UnityEditor;using System.Collections;public class CopyAllComponent : EditorWindow{    static Component[] copiedComponents;    [MenuItem("GameObject/Copy Current Components #&C")]    static void Copy()    {        copiedComponents = Selection.activeGameObject.GetComponents<Component>();    }    [MenuItem("GameObject/Paste Current Components #&P")]    static void Paste()    {        foreach (var targetGameObject in Selection.gameObjects)        {            if (!targetGameObject || copiedComponents == null) continue;            foreach (var copiedComponent in copiedComponents)            {                if (!copiedComponent) continue;                UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);            }        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

六、组件的深度拷贝粘贴源码

using UnityEngine;using UnityEditor;using System.Collections.Generic;public class DeepCopyAllComponent : EditorWindow{    [MenuItem("GameObject/Copy All Components #%&C")]    static void Copy()    {        GetAllChilds(Selection.activeGameObject,pri_my_list);    }    [MenuItem("GameObject/Paste All Components #%&P")]    static void Paste()    {        GameObject tmpGameObj = Selection.activeGameObject;        PasteChildComponent(tmpGameObj, pri_my_list);    }    public class MyComponentList    {        public MyComponentList()        {        }        public List<Component> gameObjList;        public List<MyComponentList> nextList;    }    private static void PasteChildComponent(GameObject gameObj, MyComponentList next)    {        if (next.gameObjList != null)        {            foreach (var copiedComponent in next.gameObjList)            {                if (!copiedComponent) continue;                UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gameObj);            }        }        if (next.nextList != null)        {            List<Transform> TmpListTrans = new List<Transform>();            foreach (Transform item in gameObj.transform)            {                TmpListTrans.Add(item);            }            int i = 0;            foreach (var item in next.nextList)            {                if (i < TmpListTrans.Count)                {                    PasteChildComponent(TmpListTrans[i].gameObject, item);                }                i++;            }        }    }    static MyComponentList pri_my_list = new MyComponentList();    private static void GetAllChilds(GameObject transformForSearch, MyComponentList next)    {        List<Component> childsOfGameobject = new List<Component>();        next.gameObjList = childsOfGameobject;        next.nextList = new List<MyComponentList>();        foreach (var item in transformForSearch.GetComponents<Component>())        {            childsOfGameobject.Add(item);        }        foreach (Transform item in transformForSearch.transform)        {            MyComponentList tmpnext = new MyComponentList();            GetAllChilds(item.gameObject, tmpnext);            next.nextList.Add(tmpnext);        }        return;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

七、硬广


源码的Github地址:https://github.com/cartzhang/CopyComponents

其实,也想收费的,像去Asset store上写个5刀一样。
但是,跟朋友说起来的时候,才发觉已经上传到Github上了。作为程序员的手太快了,希望分享自己的代码和成果。
程序员面薄,第一次,但是还是要挣点奶粉钱!!!!谢谢各位!!

————–THE——-END—————————————
若有问题,请随时联系!!!
再次感谢!!!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Unity中获取某个对象下面的组件
u3d快速入门图文教程
Unity网格碰撞体(MeshCollider)
[Unity 3D] Unity 3D 性能优化(二)
Unity零基础到入门 | Unity中必备组件技能学习!
【Unity开发】GameObject
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服