Unity开发中,你如果想要把某个对象的组件全部都拷贝到新的对象上,除了一个个复制粘贴组件,还要修改组件中的参数,也就是不断重复Copy Component 、Paste Component As New、Paste Component Values,实在是一件很麻烦的事,所以想办法将步骤合起来,直接复制物体上的所有组件、参数,一步搞定。
首先写一个脚本,将其放在Editor文件夹下面,代码如下:
CopyAllComponent.cs
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)
{