머터리얼 컬렉터를 수정하였습니다. 중복된 오브젝트가 많아서 소요되는 로드시간을 수정할 필요가 있다고 생각했습니다.

추가적으로 어떤 오브젝트들이 중복되었는지 출력하도록 코드를 수정하였습니다.(AI 도움을 받았습니다.ChatGPT짱!!)

 

더보기
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEditor;
using UnityEngine;

#if UNITY_EDITOR

[CustomEditor(typeof(ShaderLoader))]
public class ShaderLoaderEditor : Editor
{
    ShaderLoader ShaderLoaderThis;
    public void OnEnable()
    {
        ShaderLoaderThis = target as ShaderLoader;
    }
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        if (GUILayout.Button("Create Shader Object") == true)
        {
            ShaderLoaderThis.CreateShaderSphere();
        }

        if (GUILayout.Button("Remove Duplicate Objects") == true)
        {
            ShaderLoaderThis.RemoveDuplicateObjects();
        }
    }
}

public class ShaderLoader : MonoBehaviour
{

    public GameObject IngameField;
    [SerializeField]
    public List<string> PathList;
    [SerializeField]
    public GameObject PathString;
    [SerializeField]
    public GameObject PathObjectStore;
    // Start is called before the first frame update
    [SerializeField]
    float PosfirstX = -20.31f;
    [SerializeField]
    float PosfirstZ = 3.94f;
    float Gap = 1.5f;
    int CountCol = 12;
    int CountRow = 12;
    void Start()
    {
        CreateShaderSphere();
    }

    public void CreateShaderSphere()
    {
        int numberRow = 0;
        var ingamepos = GameObject.Find("Ingame");
        if (PathObjectStore != null)
            DestroyImmediate(PathObjectStore);
        PathObjectStore = new GameObject();
        PathObjectStore.name = "OBJStore";
        PathObjectStore.transform.SetParent(ingamepos.transform);
        for (int i = 0; i < PathList.Count; i++)
        {
            List<Material> paths = new List<Material>();
            GetDirectoriesRecursive(PathList[i], ref paths);
            //var pathString = Instantiate(PathString);
            //pathString.GetComponent<TextMeshPro>().text = PathList[i].ToString();
            //pathString.GetComponent<TextMeshPro>().alignment = TextAlignmentOptions.BottomRight;
            //pathString.transform.position = new Vector3(PosfirstX - 5f, 0f, PosfirstZ + -Gap * numberRow);
            //pathString.transform.SetParent(PathObjectStore.transform);
            for (int j = 0; j < paths.Count; j++)
            {
                var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                sphere.GetComponent<Renderer>().material = paths[j];
                numberRow = (j / CountRow);
                sphere.transform.position = new Vector3(PosfirstX + Gap * (j % CountCol), 0f, PosfirstZ + -Gap * numberRow);
                sphere.name = paths[j].name;
                sphere.transform.SetParent(PathObjectStore.transform);
            }
            numberRow++;
        }
    }


    public void RemoveDuplicateObjects()
    {
        var allObjects = PathObjectStore.GetComponentsInChildren<Transform>();
        var duplicates = new List<Transform>();
        var names = new HashSet<string>();
        var duplicateNames = new List<string>();

        for (int i = 0; i < allObjects.Length; i++)
        {
            if (names.Contains(allObjects[i].name))
            {
                duplicates.Add(allObjects[i]);
                duplicateNames.Add(allObjects[i].name);
            }
            else
            {
                names.Add(allObjects[i].name);
            }
        }

        // Keep the first object of each group and delete the rest
        for (int i = 1; i < duplicates.Count; i++)
        {
            DestroyImmediate(duplicates[i].gameObject);
        }

        // Save the list of duplicate object names to a text file
        if (duplicateNames.Count > 0)
        {
            string dateTimeString = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
            var filePath = "Assets/InGame/Scenes/SamapleShader/duplicate_names_" + dateTimeString + ".txt";
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                writer.WriteLine("Duplicate object names:");
                foreach (var name in duplicateNames)
                {
                    writer.WriteLine(name);
                }
            }
        }

    }




    // Update is called once per frame
    void Update()
    {

    }

    static private void GetDirectoriesRecursive(string path, ref List<Material> list)
    {

        string[] guids = UnityEditor.AssetDatabase.FindAssets("t:material", new string[] { path });
        List<UnityEngine.Object> returnval = new List<UnityEngine.Object>();
        Dictionary<string, int> overlapchacker = new Dictionary<string, int>();
        for (int i = 0; i < guids.Length; i++)
        {
            if (overlapchacker.ContainsKey(guids[i]))
                continue;
            overlapchacker.Add(guids[i], 1);
            var datas = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(UnityEditor.AssetDatabase.GUIDToAssetPath(guids[i]));
            foreach (var data in datas)
            {
                if (data is Material)
                {
                    list.Add(data as Material);
                }
            }

        }

    }

}
#endif

'업무일지' 카테고리의 다른 글

업무일지<GC(garbage collector)>  (2) 2023.03.09
업무일지 <유저테스트준비중>  (0) 2023.03.03
업무일지<Memory>  (0) 2023.02.17
업무일지<Tree / Occlusion>  (0) 2023.02.13
업무일지<어드레서블 시스템>  (0) 2023.02.01