您的位置:首页 > 移动开发 > Unity3D

Unity Editor 深拷贝 组件

2017-07-27 15:24 876 查看
Unity Editor 深拷贝 组件

GameObject Inspector 面板上 Copy Component 功能 的实现



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public class Copy : EditorWindow {

static Component[] componentArr;
[MenuItem("GameObject/Copy Component %#C")]
static void CopyComponent()
{
componentArr = Selection.activeGameObject.GetComponents<Component>();
}

[MenuItem("GameObject/Paste Component %#V")]
static void PasteComponent()
{
if (componentArr == null)
{
return;
}

GameObject targetGameObject = Selection.activeGameObject;
if (!targetGameObject)
{
return;
}

for (int i = 0; i < componentArr.Length; ++i)
{
Component component = componentArr[i];
if (!component)
{
continue;
}

UnityEditorInternal.ComponentUtility.CopyComponent(component);

Type type = component.GetType();
Component componentOld = targetGameObject.GetComponent(type);

if (!componentOld)
{
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);
}
else
{
UnityEditorInternal.ComponentUtility.PasteComponentValues(component);
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity