复制全部组件及Value

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using UnityEditor;

using UnityEditorInternal;

using UnityEngine;

public class ComponentCopyWindow : EditorWindow

{

private GameObject rootGO;

private List<Component> rootComponents;

private GameObject targetGO;

private List<Component> targetComponents;

private string Tag;

private Type[] ExcludeComponent = new Type[] {

typeof(MeshCollider),

typeof(MeshRenderer),

typeof(MeshFilter),

typeof(BoxCollider),

};

[MenuItem("Tools/ComponentCopyWindow")]

public static void ShowWindow()

{

var window = EditorWindow.GetWindowWithRect<ComponentCopyWindow>(new Rect(0, 0, 700, 350));

}

private void OnEnable()

{

rootComponents = new List<Component>();

targetComponents = new List<Component>();

}

[Obsolete]

private void OnGUI()

{

GUI.Label(new Rect(200, 0, 500, 20), "复制全部组件(包含Value)和Tag(不包括Transform、Mesh相关、碰撞、Layer)");

GUILayout.Space(20);

EditorGUILayout.BeginHorizontal();

{

GUILayout.Label("原始物体:");

rootGO = EditorGUILayout.ObjectField(rootGO, typeof(GameObject)) as GameObject;

if (rootGO)

{

EditorGUILayout.BeginVertical();

{

GUILayout.BeginArea(new Rect(5, 50, 300, 300), GUI.skin.box);

{

if (GUILayout.Button("检测物体组件"))

{

rootComponents = rootGO.GetComponents<Component>().ToList();

Tag = rootGO.transform.tag;

rootComponents.RemoveAll((comp) =>

{

return comp.GetType() == typeof(Transform);

});

}

if (rootComponents != null && rootComponents.Count != 0)

{

for (int i = 0; i < rootComponents.Count; i++)

{

GUILayout.Label(i + " " + rootComponents[i].GetType().Name);

}

}

}

GUILayout.EndArea();

}

EditorGUILayout.EndVertical();

}

GUILayout.Space(50);

if (rootGO && targetGO)

{

GUI.enabled = (rootComponents.Count != 0);

if (GUI.Button(new Rect(310, 160, 70, 30), "Copy to =>", EditorStyles.toolbarButton))

{

bool isExclude;

targetGO.transform.tag = Tag;

for (int i = 0; i < rootComponents.Count; i++)

{

isExclude = false;

for (int j = 0; j < ExcludeComponent.Length; j++)

{

if (rootComponents[i].GetType() == ExcludeComponent[j])

{

isExclude = true;

continue;

}

}

if (!isExclude)

{

ComponentUtility.CopyComponent(rootComponents[i]);

if (targetGO.GetComponent(rootComponents[i].GetType()) == null)

{

ComponentUtility.PasteComponentAsNew(targetGO);

}

ComponentUtility.PasteComponentValues(targetGO.GetComponent(rootComponents[i].GetType()));

}

}

targetComponents = targetGO.GetComponents<Component>().ToList();

targetComponents.RemoveAll((comp) =>

{

return comp.GetType() == typeof(Transform);

});

}

GUI.enabled = true;

}

GUILayout.Space(50);

GUILayout.Label("复制物体:");

targetGO = EditorGUILayout.ObjectField(targetGO, typeof(GameObject)) as GameObject;

if (targetGO)

{

EditorGUILayout.BeginVertical();

{

GUILayout.BeginArea(new Rect(5 + 80 + 300, 50, 300, 300), GUI.skin.box);

{

if (targetComponents != null && targetComponents.Count != 0)

{

for (int i = 0; i < targetComponents.Count; i++)

{

GUILayout.Label(i + " " + targetComponents[i].GetType().Name);

}

}

}

GUILayout.EndArea();

}

EditorGUILayout.EndVertical();

}

}

EditorGUILayout.EndHorizontal();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值