调整数组顺序使奇数位于偶数前面 交换排序方式
阅读 26 作者 傻瓜
//==============================================
//    文件:ReplacementComponent.cs
//    作者:shagua
//    日期:2019.9.28
//    功能:1.一件替换组件脚本工具类
//         2.提供单个物体组件替换和当个物体包含子物体组件替换的功能
//
//==============================================

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

public class ReplacementComponent
{
    class AllComponentList
    {
        public List<Component> gameObjList = null;
        public List<AllComponentList> nextList = null;
    }

    private static AllComponentList allComponentList = new AllComponentList();
    private static Component[] copiedComponents = null;

    [MenuItem("Tools/A Replacement Components/Copy Or Paste SingleComponents/Copy From A Obj Single Components ")]
    private static void Copy()
    {
        copiedComponents = Selection.activeGameObject.GetComponents<Component>();
        if (copiedComponents != null)
        {
            Debug.Log("Copy Single Selection.activeGameObject Components Success !!!");
        }
    }

    [MenuItem("Tools/A Replacement Components/Copy Or Paste SingleComponents/Paste To B Obj Single Components ")]
    private static void Paste()
    {
        if (copiedComponents == null) return;

        foreach (var targetGameObject in Selection.gameObjects)
        {
            if (!targetGameObject || copiedComponents == null) continue;

            foreach (Component copiedComponent in copiedComponents)
            {
                if (!copiedComponent) continue;

                UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);

                if (targetGameObject.GetComponent(copiedComponent.GetType()) is Transform) continue;

                if (targetGameObject.GetComponent(copiedComponent.GetType()) is SkinnedMeshRenderer)
                {
                    SkinnedMeshRenderer sourceskinnedMeshRenderer = copiedComponent as SkinnedMeshRenderer;


                    SkinnedMeshRenderer targetskinnedMeshRenderer =
                        targetGameObject.GetComponent(copiedComponent.GetType()) as SkinnedMeshRenderer;

                    if (sourceskinnedMeshRenderer != null)
                    {
                        targetskinnedMeshRenderer.materials = sourceskinnedMeshRenderer.materials;
                    }

                    continue;
                }

                if (targetGameObject.GetComponent(copiedComponent.GetType()))
                {
                    //如果目标组件已经存在了 不允许替换
//                    UnityEditorInternal.ComponentUtility.PasteComponentValues(
//                        targetGameObject.GetComponent(copiedComponent.GetType()));
//                    Debug.Log("PasteComponentValues" + copiedComponent.GetType().ToString() + " Success");

                    Debug.Log("<color=red>PasteComponentValues:::  目标组件已存在 不允许替换!" +
                              copiedComponent.GetType().ToString() + "</color>");
                    continue;
                }

                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);

                Debug.Log("PasteComponentAsNew" + copiedComponent.GetType().ToString() + " Success");
            }
        }
    }


    [MenuItem("Tools/A Replacement Components/Copy Or Paste AllComponents/Copy From A Obj  All Components ")]
    static void CopyAll()
    {
        GetAllChilds(Selection.activeGameObject, allComponentList);
        Debug.Log("Copy  Selection.activeGameObject All Components Success !!!");
    }


    [MenuItem("Tools/A Replacement Components/Copy Or Paste AllComponents/Paste To B Obj  All Components ")]
    static void PasteAll()
    {
        GameObject tmpGameObj = Selection.activeGameObject;
        PasteChildComponent(tmpGameObj, allComponentList);
    }

    private static void GetAllChilds(GameObject sourceGameObject, AllComponentList next)
    {
        List<Component> childsOfGameobject = new List<Component>();
        next.gameObjList = childsOfGameobject;
        next.nextList = new List<AllComponentList>();

        foreach (var item in sourceGameObject.GetComponents<Component>())
        {
            childsOfGameobject.Add(item);
        }

        foreach (Transform item in sourceGameObject.transform)
        {
            AllComponentList tmpnext = new AllComponentList();
            GetAllChilds(item.gameObject, tmpnext);
            next.nextList.Add(tmpnext);
        }
    }


    private static void PasteChildComponent(GameObject gameObj, AllComponentList next)
    {
        if (next.gameObjList != null)
        {
            foreach (var copiedComponent in next.gameObjList)
            {
                if (!copiedComponent) continue;

                UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);

                if (gameObj.GetComponent(copiedComponent.GetType()) is Transform) continue;

                if (gameObj.GetComponent(copiedComponent.GetType()) is SkinnedMeshRenderer)
                {
                    SkinnedMeshRenderer sourceskinnedMeshRenderer = copiedComponent as SkinnedMeshRenderer;


                    SkinnedMeshRenderer targetskinnedMeshRenderer =
                        gameObj.GetComponent(copiedComponent.GetType()) as SkinnedMeshRenderer;

                    if (sourceskinnedMeshRenderer != null)
                    {
                        targetskinnedMeshRenderer.materials = sourceskinnedMeshRenderer.materials;
                    }

                    continue;
                }

                if (gameObj.GetComponent(copiedComponent.GetType()))
                {
                    //如果目标组件已经存在了 不允许替换
//                    UnityEditorInternal.ComponentUtility.PasteComponentValues(
//                        gameObj.GetComponent(copiedComponent.GetType()));
//
//                    Debug.Log("PasteChildComponent===>PasteComponentValues" + copiedComponent.GetType().ToString() +
//                              " Success");

                    Debug.Log("<color=red>PasteComponentValues:::  目标组件已存在 不允许替换!" +
                              copiedComponent.GetType().ToString() + "</color>");
                    continue;
                }

                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gameObj);
                Debug.Log("PasteChildComponent===>PasteComponentAsNew" + copiedComponent.GetType().ToString() +
                          " Success");
            }
        }

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