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>();
}
一句话,就是获取当前选中的对象的组件,然后存储于变量中。
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;
}
}
七、硬广
源码的Github地址:https://github.com/cartzhang/CopyComponents
其实,也想收费的,像去Asset store上写个5刀一样。
但是,跟朋友说起来的时候,才发觉已经上传到Github上了。作为程序员的手太快了,希望分享自己的代码和成果。
程序员面薄,第一次,但是还是要挣点奶粉钱!!!!谢谢各位!!
————–THE——-END—————————————
若有问题,请随时联系!!!
再次感谢!!!
Unity对象的所有组件深拷贝与粘贴的更多相关文章
- Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...
- Unity3D热更新之LuaFramework篇[07]--怎么让unity对象绑定Lua脚本
前言 在上一篇文章 Unity3D热更新之LuaFramework篇[06]--Lua中是怎么实现脚本生命周期的 中,我分析了由LuaBehaviour来实现lua脚本生命周期的方法. 但在实际使用中 ...
- Unity3d修炼之路:载入一个预制体,然后为该对象加入组件,然后查找对象,得到组件。
#pragma strict function Awake(){ //载入一个预制体 资源必须在 Resources目录下 Resources.LoadLoad(); //载入后 必须演示样例化 Ga ...
- Unity 对象池 生产 保存
Unity对象池主要是保存那些常用的物体,避免他们在不断销毁和创造中损坏性能. 主要思路为:创造物体时,判断是否存在,如果存在则调用并使其显示.如果不存在则创造一个新的. 当销毁时,调用协程延时隐藏物 ...
- Angular通过订阅观察者对象实现不同组件中数据的实时传递
在angular官方定义中,组件直接的数据交换只要在父子直接传递,但是我们在项目中经常需要在各种层级之间传递数据,下面介绍关于订阅可观察对象实现的数据传递. 首先定义一个服务app.sevice.ts ...
- Java对象的浅拷贝和深拷贝&&String类型的赋值
Java中的数据类型分为基本数据类型和引用数据类型.对于这两种数据类型,在进行赋值操作.方法传参或返回值时,会有值传递和引用(地址)传递的差别. 浅拷贝(Shallow Copy): ①对于数据类型是 ...
- 第二百四十一节,Bootstrap进度条媒体对象和 Well 组件
第二百四十一节,Bootstrap进度条媒体对象和 Well 组件 学习要点: 1.Well 组件 2.进度条组件 3.媒体对象组件 本节课我们主要学习一下 Bootstrap 的三个组件功能:Wel ...
- Unity TextMeshPro替代Text组件创建简体中文字体纹理集
Unity原生的Text组件有一个毛病,只要文本放大字体放大就会有毛边或锯齿,一个更好的解决方案是用TextMeshPro替代ugui中的Text组件. TMPro采用SDF文字渲染技术,可以使文字放 ...
- JAVA中对象的克隆及深拷贝和浅拷贝
使用场景: 在日常的编程过程 中,经常会遇到,有一个对象OA,在某一时间点OA中已经包含了一些有效值 ,此时可能会需一个和OA完全相对的新对象OB,并且要在后面的操作中对OB的任何改动都不会影响到OA ...
随机推荐
- sql server 大数据处理
对SQL Server数据表进行分区的过程分为三个步骤: 1)建立分区函数 2)建立分区方案 3)对表格进行分区 第一个步骤:建立分区函数 分区函数定义[u]how[/u],即你想要SQL Serve ...
- [C++ 多线程] 学习前瞻
1 多线程是什么? 1.1 多线程的概念? 说起多线程,那么就不得不说什么是线程,而说起线程,又不得不说什么是进程. 进程可以简单的理解为一个可以独立运行的程序单位,它是线程的集合,进程就是有一个或多 ...
- [Usaco2018 Open]Disruption
Description Farmer John自豪于他所经营的交通发达的的农场.这个农场是由N块牧场(2≤N≤50,000)组成的,N-1条双向道路将它们连接起来,每一条道路的都为一单位长度.Farm ...
- ACM_物品分堆(01背包)
物品分堆 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个物品,物品i的重量为Wi,现在想要把这个n个物品分类两堆,求最小的 ...
- Python Turtle绘图
1. 画布(canvas) 画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置 1.1 设置画布大小 turtle.screensize(canvwidth=None, ca ...
- Vue组件之间通信的三种方式
最近在看梁颠编著的<Vue.js实战>一书,感觉颇有收获,特此记录一些比价实用的技巧. 组件是MVVM框架的核心设计思想,将各功能点组件化更利于我们在项目中复用,这类似于我们服务端面向对象 ...
- 预采订单管理接收来源App数据
- Modbus通讯错误检测方法
标准的Modbus串行网络采用两种错误检测方法.奇偶校验对每个字符都可用,帧检测(LRC和CRC)应用于整个消息.它们都是在消息发送前由主设备产生的,从设备在接收过程中检测每个字符和整个消息帧. 用户 ...
- python--12、pymysql模块
安装 pip3 install pymysql 使用方法(示例表为用户注册信息表): import pymysql user=input('username: ').strip() pwd=input ...
- Python爬虫+颜值打分,5000+图片找到你的Mrs. Right
一见钟情钟的不是情,是脸 日久生情生的不是脸,是情 项目简介 本项目利用Python爬虫和百度人脸识别API,针对简书交友专栏,爬取用户照片(侵删),并进行打分. 本项目包括以下内容: 图片爬 ...