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

发表于2017-11-11
评论0 2.9k浏览
下面给大家分享的是在Unity开发中对象的所有组件深拷贝与粘贴,为了让大家可以了解的更明白,会从单个组件拷贝开始介绍,让大家可以一步步的去了解。

一、Unity 单个组件拷贝

Unity中,经常有对象或预制体,但是想要把某个预制体或对象的组件全部都拷贝到某个新的对象上。 
Unity 虽然已经提供了Copy Component这个功能, 
 
这个功能很不错,可以拷贝粘贴,但是面对某个组件上大量的组件和里面的各种参数调整, 
 
对象或预制体的层级结构深,各层的组件多,参数也纷繁复杂,这个就不太灵光了。 

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

方法很简单的,不就是复制和粘贴么,两步走。 
1.拷贝

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

一句话,就是获取当前选中的对象的组件,然后存储于变量中。 
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);
            }
        }
    }

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

CopyComponent
PasteComponentAsNew

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

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

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

public class MyComponentList
    {
        public MyComponentList()
        {
        }
        public List<Component> gameObjList;
        public List<MyComponentList> nextList;
    }


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

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

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

四、运行截图

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

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);
            }
        }
    }
}
六、组件的深度拷贝粘贴源码

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

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论