Unity Editor 小工具集合(持续更新)

1.LOD批量设置

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

public class LODModelSet : EditorWindow
{
    public GameObject rootObj;

    public GameObject lowAndMiddleRootObj;

    public float highLevel, middleLevel, lowLevel;

    [MenuItem("Tools/设置模型LOD")]
    public static void showWindow()
    {
        EditorWindow.CreateInstance<LODModelSet>().Show();
    }

    private void OnGUI()
    {
        EditorGUILayout.LabelField("高清模型根节点");
        rootObj = EditorGUILayout.ObjectField(rootObj, typeof(GameObject), true) as GameObject;
        EditorGUILayout.LabelField("中低模型根节点");
        lowAndMiddleRootObj = EditorGUILayout.ObjectField(lowAndMiddleRootObj, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("设置LOD层级"))
        {
            SetLod();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("自动打组根节点");
        groupRoot = EditorGUILayout.ObjectField(groupRoot, typeof(GameObject), true) as GameObject;
        EditorGUILayout.LabelField("基础名称长度");
        baseNameLength = EditorGUILayout.IntField(baseNameLength);

        if (GUILayout.Button("自动打组"))
        {
            GroupModels();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("LOD检查");
        checkRoot = EditorGUILayout.ObjectField(checkRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("LOD检查"))
        {
            ClearNullInLod();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("LOD低层次模型提取");
        getlowLodRoot = EditorGUILayout.ObjectField(getlowLodRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("LOD低层次模型提取"))
        {
            GetAllLowModels();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("LOD高层次模型提取");
        gethightLodRoot = EditorGUILayout.ObjectField(gethightLodRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("LOD高层次模型提取"))
        {
            GetAllHighModels();
        }

        if (GUILayout.Button("特殊LOD高层及模型提取"))
        {
            GetSpicalHighLodModels();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("LOD层次模型-高");
        highRoot = EditorGUILayout.ObjectField(highRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("LOD层次模型合并打组"))
        {
            MergeLods();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("查找低精度模型并并入LOD中");
        lowModelRoot = EditorGUILayout.ObjectField(lowModelRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("查找低精度模型并并入LOD中"))
        {
            FindLowModelAndSet();
        }
    }

    private void SetLod()
    {
        List<Transform> highModels = new List<Transform>();

        for (int i = 0; i < rootObj.transform.childCount; i++)
        {
            highModels.Add(rootObj.transform.GetChild(i));
        }

        highModels.RemoveAll(s => s.GetComponentsInChildren<MeshRenderer>().Length == 0 || s.GetComponentsInChildren<MeshRenderer>() == null);

        List<Transform> lowModels = new List<Transform>();

        List<Transform> lowmiddleModes = new List<Transform>();

        for (int i = 0; i < lowAndMiddleRootObj.transform.childCount; i++)
        {
            lowmiddleModes.Add(lowAndMiddleRootObj.transform.GetChild(i));
        }

        for (int i = 0; i < highModels.Count; i++)
        {
            string baseName = highModels[i].name;

            var lowModel = lowmiddleModes.Find(s => s.name.Contains(baseName) && s.name.Contains("_Low"));

            if (lowModel != null)
            {
                lowModels.Add(lowModel);
            }
            else
            {
                Debug.Log(baseName + "没有找到低模");
            }
        }

        for (int i = 0; i < highModels.Count; i++)
        {
            string baseName = highModels[i].name;

            var lowm = lowModels.Find(s => s.name.Contains(baseName));
            if (lowm == null)
            {
                continue;
            }

            GameObject highm = highModels[i].gameObject;

            GameObject newP = new GameObject(baseName);

            newP.transform.SetParent(rootObj.transform);

            newP.transform.localPosition = Vector3.zero;
            newP.transform.localEulerAngles = Vector3.zero;
            newP.transform.localScale = Vector3.one;

            LODGroup group = newP.AddComponent<LODGroup>();

            LOD[] lODs = new LOD[2];

            highm.transform.SetParent(newP.transform);
            lODs[0] = new LOD(0.2f, highm.GetComponentsInChildren<MeshRenderer>());

            lODs[1] = new LOD(0f, lowm.GetComponentsInChildren<MeshRenderer>());

            group.SetLODs(lODs);
        }
    }

    #region 同一个格式的名称的模型的自动打组
    public GameObject groupRoot;

    public int baseNameLength;

    private void GroupModels()
    {
        List<Transform> childrens = new List<Transform>();

        for (int i = 0; i < groupRoot.transform.childCount; i++)
        {
            childrens.Add(groupRoot.transform.GetChild(i));
        }

        List<Transform> groups = new List<Transform>();

        for (int i = 0; i < childrens.Count; i++)
        {
            string basename = childrens[i].name.Substring(0, baseNameLength);

            var result = groups.Find(s => s.name.Equals(basename));

            if (result == null)
            {
                GameObject p = new GameObject(basename);

                p.transform.SetParent(groupRoot.transform);

                result = p.transform;

                groups.Add(result);
            }

            childrens[i].transform.SetParent(result);
        }
    }
    #endregion

    #region 检查LOD内是否有空物体并去除
    public GameObject checkRoot;

    private void ClearNullInLod()
    {
        LODGroup[] lODGroups = checkRoot.GetComponentsInChildren<LODGroup>();

        for (int i = 0; i < lODGroups.Length; i++)
        {
            LODGroup group = lODGroups[i];

            LOD[] lods = group.GetLODs();

            for (int j = 0; j < lods.Length; j++)
            {
                LOD lod = lods[j];

                for (int k = 0; k < lod.renderers.Length; k++)
                {
                    if (lod.renderers[k] == null)
                    {
                        Debug.Log(group.transform.name);
                    }
                }
            }
        }
    }
    #endregion

    #region 还原并拆分LOD模型

    public GameObject getlowLodRoot, gethightLodRoot;

    private void GetAllLowModels()
    {
        GameObject newRoot = new GameObject(getlowLodRoot.name + "_Lod");

        newRoot.transform.SetParent(getlowLodRoot.transform.parent);

        LODGroup[] lODGroups = getlowLodRoot.GetComponentsInChildren<LODGroup>();

        for (int i = 0; i < lODGroups.Length; i++)
        {
            LODGroup group = lODGroups[i];
            LOD[] lods = group.GetLODs();
            LOD lowLod = lods[1];

            for (int j = 0; j < lowLod.renderers.Length; j++)
            {
                if (lowLod.renderers[j] != null)
                {
                    lowLod.renderers[j].transform.SetParent(newRoot.transform);
                }
            }
        }
    }

    private void GetAllHighModels()
    {
        GameObject newRoot = new GameObject(gethightLodRoot.name + "_High");

        newRoot.transform.SetParent(gethightLodRoot.transform.parent);

        LODGroup[] lODGroups = gethightLodRoot.GetComponentsInChildren<LODGroup>();

        for (int i = 0; i < lODGroups.Length; i++)
        {
            LODGroup group = lODGroups[i];
            LOD[] lods = group.GetLODs();
            LOD lowLod = lods[0];

            for (int j = 0; j < lowLod.renderers.Length; j++)
            {
                if (lowLod.renderers[j] != null)
                {
                    lowLod.renderers[j].transform.SetParent(newRoot.transform);
                }
            }
        }
    }
    #endregion

    #region 各个层级的lod模型合并
    public GameObject highRoot, middleRoot, lowRoot;

    private void MergeLods()
    {
        LODGroup[] lODGroups = highRoot.GetComponentsInChildren<LODGroup>();

        for (int i = 0; i < lODGroups.Length; i++)
        {
            LODGroup group = lODGroups[i];

            Transform parent = group.transform;

            LOD[] lods = group.GetLODs();

            for (int j = 0; j < lods[1].renderers.Length; j++)
            {
                if (lods[1].renderers[j] != null)
                {
                    lods[1].renderers[j].transform.SetParent(parent);
                }
            }
        }
    }
    #endregion

    #region 查找低精度模型并并入LOD种
    public GameObject lowModelRoot;

    private void FindLowModelAndSet()
    {
        LODGroup[] groups = lowModelRoot.GetComponentsInChildren<LODGroup>(true);

        for (int i = 0; i < groups.Length; i++)
        {
            LODGroup group = groups[i];

            string lowname = group.name + "_Low";

            GameObject low = GameObject.Find("Model/资源/GIS/LOD/" + lowname);

            if (low == null)
            {
                Debug.LogError("Model/资源/GIS/LOD/" + lowname + ",找不到该对象");
            }
            else
            {
                MeshRenderer[] renderers = low.GetComponentsInChildren<MeshRenderer>();

                LOD[] lods = new LOD[2];

                lods[0] = new LOD(0.15f, group.GetLODs()[0].renderers);

                lods[1] = new LOD(0, renderers);

                group.SetLODs(lods);
            }
        }
    }
    #endregion

    #region 提取剩余模型(LOD高精度模型属于空物体父物体下)
    private void GetSpicalHighLodModels()
    {
        GameObject newPa = new GameObject("SpicalHighLodModels");

        LODGroup[] lODGroups = gethightLodRoot.GetComponentsInChildren<LODGroup>(true);

        List<Transform> transforms = new List<Transform>();

        for (int i = 0; i < lODGroups.Length; i++)
        {
            Transform a = lODGroups[i].transform;

            if (a.childCount > 0)
            {
                for (int j = a.childCount - 1; j >= 0; j--)
                {
                    transforms.Add(a.GetChild(j));
                }
            }
        }

        foreach (var item in transforms)
        {
            item.SetParent(newPa.transform);
        }
    }
    #endregion
}

2.重复模型的相关操作

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

public class DuplicateHandle : EditorWindow
{
    [MenuItem("Tools/Duplicate Handle")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(DuplicateHandle));
    }

    private void OnGUI()
    {
        hideRoot = EditorGUILayout.ObjectField("包含隐藏对象的根节点", hideRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("输出重复模型名称"))
        {
            OutputHideModels();
        }

        textName = EditorGUILayout.TextField("需要隐藏的模型名称列表文件", textName);

        modelRoot = EditorGUILayout.ObjectField("包含隐藏对象的根节点", modelRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("隐藏重复模型"))
        {
            HideModelsByText();
        }

        dulicateRoot = EditorGUILayout.ObjectField("根节点", dulicateRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("整理隐藏模型"))
        {
            CalculateModels();
        }

        allRoot = EditorGUILayout.ObjectField("根节点", allRoot, typeof(GameObject), true) as GameObject;

        groupName = EditorGUILayout.TextField("分组名称", groupName);

        if (GUILayout.Button("分组"))
        {
            GroupModelsByName();
        }

        rootObj1 = EditorGUILayout.ObjectField("根节点1", rootObj1, typeof(GameObject), true) as GameObject;

        rootObj2 = EditorGUILayout.ObjectField("根节点2", rootObj2, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("删除已有模型"))
        {
            DeletSameModel();
        }
    }

    public GameObject hideRoot;

    private void OutputHideModels()
    {
        string hideSTr = "";

        List<GameObject> hideObjs = new List<GameObject>();

        for (int i = 0; i < hideRoot.transform.childCount; i++)
        {
            if (!hideRoot.transform.GetChild(i).gameObject.activeInHierarchy)
            {
                hideSTr += hideRoot.transform.GetChild(i).name + "\n";

                hideObjs.Add(hideRoot.transform.GetChild(i).gameObject);
            }
        }

        if (!File.Exists(Application.streamingAssetsPath + "/HideModels" + hideRoot.name + ".txt"))
        {
            File.Create(Application.streamingAssetsPath + "/HideModels" + hideRoot.name + ".txt").Close();
        }

        File.WriteAllText(Application.streamingAssetsPath + "/HideModels" + hideRoot.name + ".txt", hideSTr);
    }

    public string textName;

    public GameObject modelRoot;

    private void HideModelsByText()
    {
        string ss = File.ReadAllText(Application.streamingAssetsPath + "/" + textName + ".txt");

        string[] s = ss.Split("\n");

        for (int i = 0; i < s.Length; i++)
        {
            string n = s[i];

            string baseName = n.Substring(0, 14);

            for (int j = 0; j < modelRoot.transform.childCount; j++)
            {
                if (modelRoot.transform.GetChild(j).name == baseName)
                {
                    modelRoot.transform.GetChild(j).gameObject.SetActive(false);
                }
            }
        }
    }

    public GameObject dulicateRoot;

    private void CalculateModels()
    {
        List<GameObject> models = new List<GameObject>();

        for (int i = 0; i < dulicateRoot.transform.childCount; i++)
        {
            if (!dulicateRoot.transform.GetChild(i).gameObject.activeSelf)
            {
                models.Add(dulicateRoot.transform.GetChild(i).gameObject);
            }
        }

        GameObject a = new GameObject(dulicateRoot.name + "_Hide");

        for (int i = 0; i < models.Count; i++)
        {
            models[i].transform.SetParent(a.transform);
        }
    }

    public GameObject allRoot;

    public string groupName;

    private void GroupModelsByName()
    {
        GameObject a = new GameObject(groupName);

        List<GameObject> groupModels = new List<GameObject>();

        for (int i = 0; i < allRoot.transform.childCount; i++)
        {
            if (allRoot.transform.GetChild(i).name.Contains(groupName))
            {
                groupModels.Add(allRoot.transform.GetChild(i).gameObject);
            }
        }

        for (int i = 0; i < groupModels.Count; i++)
        {
            groupModels[i].transform.SetParent(a.transform);
        }
    }

    [MenuItem("Tools/Calculate Selects")]
    public static void CalculateSelects()
    {
        GameObject[] selectes = Selection.gameObjects;

        if (!File.Exists(Application.streamingAssetsPath + "/waterModels.txt"))
        {
            File.Create(Application.streamingAssetsPath + "/waterModels.txt").Close();
        }

        string a = "";

        for (int i = 0; i < selectes.Length; i++)
        {
            a += selectes[i].name + "\n";
        }

        File.WriteAllText(Application.streamingAssetsPath + "/waterModels.txt", a);
    }

    public GameObject rootObj1, rootObj2;

    private void DeletSameModel()
    {
        List<GameObject> models1 = new List<GameObject>();

        List<GameObject> models2 = new List<GameObject>();

        for (int i = 0; i < rootObj1.transform.childCount; i++)
        {
            models1.Add(rootObj1.transform.GetChild(i).gameObject);
        }

        for (int i = 0; i < rootObj2.transform.childCount; i++)
        {
            models2.Add(rootObj2.transform.GetChild(i).gameObject);
        }

        var resultAll = models2.FindAll(s => models1.Contains(s));

        for (int i = resultAll.Count -1; i >= 0; i--)
        {
            Debug.Log(resultAll[i].name + "已经存在了");

            //DestroyImmediate(resultAll[i]);
        }
    }
}

3.批量重命名与后缀的增删

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

public class NameEditor : EditorWindow
{
    public GameObject root;

    public string namePrefix = "New";

    [MenuItem("Tools/Name Editor")]
    public static void ShowWindow()
    {
        // 创建并显示窗口
        EditorWindow.GetWindow(typeof(NameEditor));
    }

    private void OnGUI()
    {
        root = EditorGUILayout.ObjectField("父对象", root, typeof(GameObject), true) as GameObject;

        namePrefix = EditorGUILayout.TextField("名称后缀", namePrefix);

        if (GUILayout.Button("重命名"))
        {
            AddSameAfterName();
        }

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("名称修改根节点");
        cutNameRoot = EditorGUILayout.ObjectField(cutNameRoot, typeof(GameObject), true) as GameObject;
        EditorGUILayout.LabelField("名称长度");
        cutLength = EditorGUILayout.IntField(cutLength);

        if (GUILayout.Button("名称修改(截取)"))
        {
            CutNames();
        }
        EditorGUILayout.LabelField("需要去除的名称后缀");
        cutStr = EditorGUILayout.TextField(cutStr);

        if (GUILayout.Button("名称修改(去除后缀)"))
        {
            CutNamesForStr();
        }
    }

    private void AddSameAfterName()
    {
        for (int i = 0; i < root.transform.childCount; i++)
        {
            root.transform.GetChild(i).name += namePrefix;
        }
    }

    #region 修改名称长度
    public GameObject cutNameRoot;

    public int cutLength;

    private void CutNames()
    {
        for (int i = 0; i < cutNameRoot.transform.childCount; i++)
        {
            cutNameRoot.transform.GetChild(i).name = cutNameRoot.transform.GetChild(i).name.Substring(0, cutLength);
        }
    }
    #endregion

    #region 去除指定后缀
    public string cutStr;

    private void CutNamesForStr()
    {
        for (int i = 0; i < cutNameRoot.transform.childCount; i++)
        {
            cutNameRoot.transform.GetChild(i).name = cutNameRoot.transform.GetChild(i).name.Replace(cutStr, "");
        }
    }
    #endregion
}

4.大型扫描模型场景高精度模型区块划分

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

public class ChunkCreate : EditorWindow
{
    public GameObject originalRoot;

    public GameObject allModelRoot;

    public GameObject chunkRoot;

    public float chunkSize = 10f;

    [MenuItem("Tools/ChunkCreate")]
    public static void ShowWindow()
    {
        EditorWindow.CreateInstance<ChunkCreate>().Show();
    }

    private void OnGUI()
    {
        EditorGUILayout.LabelField("块划分起始点");
        originalRoot = EditorGUILayout.ObjectField(originalRoot, typeof(GameObject), true) as GameObject;

        EditorGUILayout.LabelField("所有模型");
        allModelRoot = EditorGUILayout.ObjectField(allModelRoot, typeof(GameObject), true) as GameObject;

        EditorGUILayout.LabelField("块大小");
        chunkSize = EditorGUILayout.FloatField(chunkSize);

        if (GUILayout.Button("ChunkCreate"))
        {
            CreateChunke();
        }

        EditorGUILayout.LabelField("块划分排序");
        chunkRoot = EditorGUILayout.ObjectField(chunkRoot, typeof(GameObject), true) as GameObject;

        if (GUILayout.Button("Chunk Sort"))
        {
            SortChunke();
        }

        if (GUILayout.Button("Empty Chunk Node"))
        {
            CreateEmptyNodeForPos();
        }

        if (GUILayout.Button("Back"))
        {
            BackForBefore();
        }

        if (GUILayout.Button("Back Chunk"))
        {
            BackForChunk();
        }

        if (GUILayout.Button("PreSetLowModels"))
        {
            InitMainLowModels();
        }

        EditorGUILayout.LabelField("实例化BIM周边区块");
        EditorGUILayout.LabelField("视角对象");
        viewTrans = EditorGUILayout.ObjectField(viewTrans, typeof(GameObject), true) as GameObject;
        EditorGUILayout.LabelField("区块原点");
        chunkStartObj = EditorGUILayout.ObjectField(chunkStartObj, typeof(GameObject), true) as GameObject;
        EditorGUILayout.LabelField("BIM区块父对象");
        BIMChunkParent = EditorGUILayout.ObjectField(BIMChunkParent, typeof(GameObject), true) as GameObject;
        EditorGUILayout.LabelField("BIM区块半径数量");
        chunkNum = EditorGUILayout.IntField(chunkNum);

        if (GUILayout.Button("Create BIM Chunk"))
        {
            CalculateViewChunks();
        }

        if (GUILayout.Button("Change BIM Chunk"))
        {
            ChangeMainChunk();
        }

        EditorGUILayout.LabelField("测试中心计算");
        testObj = EditorGUILayout.ObjectField(testObj, typeof(GameObject), true) as GameObject;
        if (GUILayout.Button("中心点测试"))
        {
            Vector3 pos = CalculateCenterPos(testObj.transform);
            GameObject a = new GameObject("test");
            a.transform.position = pos;
        }
    }

    private void CreateChunke()
    {
        Vector3 startPos = originalRoot.transform.position;

        Dictionary<string, List<GameObject>> chunkDic = new Dictionary<string, List<GameObject>>();

        Dictionary<string, List<Vector3>> chunkPosDic = new Dictionary<string, List<Vector3>>();



        ///划分
        for (int x = 0; x < allModelRoot.transform.childCount; x++)
        {
            Vector3 pos = CalculateCenterPos(allModelRoot.transform.GetChild(x).transform);

            int chunkIndex_X = (int)(MathF.Abs(pos.x - startPos.x) / chunkSize);

            int chunkIndex_Y = (int)(MathF.Abs(pos.z - startPos.z) / chunkSize);

            string chunkIndex = chunkIndex_X + "_" + chunkIndex_Y;

            if (chunkDic.ContainsKey(chunkIndex))
            {
                chunkDic[chunkIndex].Add(allModelRoot.transform.GetChild(x).gameObject);

                chunkPosDic[chunkIndex].Add(pos);
            }
            else
            {
                List<GameObject> list = new List<GameObject>();
                list.Add(allModelRoot.transform.GetChild(x).gameObject);
                chunkDic.Add(chunkIndex, list);

                List<Vector3> listPos = new List<Vector3>();
                listPos.Add(pos);
                chunkPosDic.Add(chunkIndex, listPos);
            }
        }

        List<GameObject> nodes = new List<GameObject>();

        foreach (var item in chunkDic)
        {
            GameObject node = new GameObject("Chunk_" + item.Key);

            node.transform.SetParent(allModelRoot.transform.parent);

            ChunkNode chunkNode = node.AddComponent<ChunkNode>();

            int x = int.Parse(item.Key.Split('_')[0]);
            int y = int.Parse(item.Key.Split('_')[1]);

            chunkNode.SetMsg(x, y);

            nodes.Add(node);
        }

        foreach (var item in chunkPosDic)
        {
            var result = nodes.Find(x => x.name == "Chunk_" + item.Key);

            if (result != null)
            {
                Vector3 pos = Vector3.zero;

                for (int i = 0; i < item.Value.Count; i++)
                {
                    pos += item.Value[i];
                }

                result.transform.position = pos / item.Value.Count;
            }
        }

        foreach (var item in chunkDic)
        {
            var result = nodes.Find(x => x.name == "Chunk_" + item.Key);

            if (result != null)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    item.Value[i].transform.SetParent(result.transform);
                }
            }
        }
    }

    private void SortChunke()
    {
        List<ChunkNode> nodes = new List<ChunkNode>();

        for (int i = 0; i < chunkRoot.transform.childCount; i++)
        {
            Transform child = chunkRoot.transform.GetChild(i);

            ChunkNode chunkNode = child.gameObject.AddComponent<ChunkNode>();

            string num = child.name.Split('_')[1];

            int x = int.Parse(child.name.Split('_')[1]);

            int y = int.Parse(child.name.Split('_')[2]);

            chunkNode.SetMsg(x, y);

            nodes.Add(chunkNode);
        }

        Dictionary<int, ChunkNode> dic = new Dictionary<int, ChunkNode>();

        foreach (var item in nodes)
        {
            dic.Add(item.GetMsg().Item1 * 100 + item.GetMsg().Item2, item);
        }

        dic = dic.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);

        GameObject newEmpty = new GameObject("EMPTY_New");

        foreach (var item in dic)
        {
            item.Value.transform.SetParent(newEmpty.transform);
        }
    }

    private void CreateEmptyNodeForPos()
    {
        GameObject EMPTY = new GameObject("EMPTY");

        for (int i = 0; i < chunkRoot.transform.childCount; i++)
        {
            ChunkNode node = chunkRoot.transform.GetChild(i).GetComponent<ChunkNode>();

            GameObject a = new GameObject(node.transform.name);

            a.transform.SetParent(EMPTY.transform);

            ChunkNode aaa = a.AddComponent<ChunkNode>();

            aaa.SetMsg(node.xIndex, node.yIndex);

            a.transform.position = node.transform.position;
        }
    }

    public GameObject testObj;

    public Vector3 CalculateCenterPos(Transform trans)
    {
        Renderer[] renderers = trans.GetComponentsInChildren<Renderer>(true);

        Vector3 centerPos = Vector3.zero;

        for (int i = 0; i < renderers.Length; i++)
        {
            centerPos += renderers[i].bounds.center;
        }

        return centerPos / renderers.Length;
    }

    private void BackForBefore()
    {
        LODGroup[] lODGroups = chunkRoot.GetComponentsInChildren<LODGroup>();

        for (int i = 0; i < lODGroups.Length; i++)
        {
            lODGroups[i].transform.SetParent(chunkRoot.transform);
        }
    }

    private void BackForChunk()
    {
        ChunkLogic[] trans = chunkRoot.GetComponentsInChildren<ChunkLogic>(true);

        for (int i = 0; i < trans.Length; i++)
        {
            for (int j = trans[i].transform.childCount - 1; j >= 0; j--)
            {
                DestroyImmediate(trans[i].transform.GetChild(j).gameObject);
            }
        }
    }

    private void InitMainLowModels()
    {
        ChunkLogic[] chunkLogics = chunkRoot.GetComponentsInChildren<ChunkLogic>();

        for (int i = 0; i < chunkLogics.Length; i++)
        {
            if (chunkLogics[i].isMainChunk)
            {
                chunkLogics[i].SetLowModelsBeforePlay();
            }
        }
    }

    public GameObject viewTrans;

    public GameObject chunkStartObj;

    public GameObject BIMChunkParent;

    public int chunkNum = 0;

    class ChunkIndex
    {
        internal int x;
        internal int y;
    }

    private void CalculateViewChunks()
    {
        int x = (int)(Mathf.Abs(viewTrans.transform.position.x - chunkStartObj.transform.position.x) / 100);

        int y = (int)(Mathf.Abs(viewTrans.transform.position.z - chunkStartObj.transform.position.z) / 100);

        ChunkIndex index = new ChunkIndex();
        index.x = x;
        index.y = y;

        List<ChunkIndex> indexList2 = new List<ChunkIndex>();
        int minx = x - chunkNum > 0 ? x - chunkNum : 0;
        int maxx = x + chunkNum <= 37 ? x + chunkNum : 37;
        int miny = y - chunkNum > 0 ? y - chunkNum : 0;
        int maxy = y + chunkNum <= 57 ? y + chunkNum : 57;

        for (int j = minx; j <= maxx; j++)
        {
            for (int k = miny; k <= maxy; k++)
            {
                ChunkIndex chunkIndex2 = new ChunkIndex();
                chunkIndex2.x = j;
                chunkIndex2.y = k;

                var result = indexList2.Find(s => s.x == chunkIndex2.x && s.y == chunkIndex2.y);
                if (result == null)
                {
                    indexList2.Add(chunkIndex2);
                }
            }
        }

        foreach (var item in indexList2)
        {
            string fileName = "Chunk" + "_" + item.x + "_" + item.y;

            if (BIMChunkParent.transform.Find(fileName) != null)
            {
                Debug.Log(fileName + "已经加载过了");

                continue;
            }
            else
            {
                Debug.Log("GIS/" + fileName + "需要被加载");
            }

            var newObj = Resources.Load("GIS/" + fileName) as GameObject;

            if (newObj == null)
            {
                Debug.Log("GIS/" + fileName + "不存在");
            }
            else
            {
                GameObject newObject = Instantiate(newObj);

                newObject.transform.SetParent(BIMChunkParent.transform);
            }
        }
    }

    private void ChangeMainChunk()
    {
        LODGroup[] lODGroups = BIMChunkParent.GetComponentsInChildren<LODGroup>(true);

        for (int i = 0; i < lODGroups.Length; i++)
        {
            lODGroups[i].enabled = false;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值