| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | using UnityEngine;using UnityEditor;using System.Collections.Generic;public class InstantiateQuadsEditor : EditorWindow{    [MenuItem("Tools/Instantiate Quads")]    public static void ShowWindow()    {        GetWindow<InstantiateQuadsEditor>("Instantiate Quads");    }    private GameObject parentObject; // 父对象    private GameObject prefab;    private int hor = 3; // 水平分割数量    private int ver = 2; // 垂直分割数量    void OnGUI()    {        GUILayout.Label("Instantiate Quads Settings", EditorStyles.boldLabel);        // 显示父对象选择器        parentObject = (GameObject)EditorGUILayout.ObjectField("Parent Object", parentObject, typeof(GameObject), true);        // 显示Prefab选择器        prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);        // 显示水平和垂直分割数量输入框        hor = EditorGUILayout.IntField("Horizontal Splits", hor);        ver = EditorGUILayout.IntField("Vertical Splits", ver);        // 添加按钮        if (GUILayout.Button("Instantiate Quads"))        {            if (parentObject == null)            {                Debug.LogError("Parent Object is not assigned.");                return;            }            InstantiateQuads(prefab, hor, ver, parentObject.transform);        }    }    private Material CreateAndSaveMaterial(Texture texture, string assetFolderPath)    {        if (texture == null)        {            Debug.LogError("Texture is null");            return null;        }        // 确保 assetFolderPath 是有效的 Unity 文件夹路径        if (string.IsNullOrEmpty(assetFolderPath) || !AssetDatabase.IsValidFolder(assetFolderPath))        {            Debug.LogError("Invalid asset folder path: " + assetFolderPath);            return null;        }        // 创建一个新的材质        Material mat = new Material(Shader.Find("Unlit/Texture"));        if (mat == null)        {            Debug.LogError("Shader 'Unlit/Texture' not found");            return null;        }        // 设置材质的纹理        mat.mainTexture = texture;        // 为材质创建一个唯一的名称        string materialName = "Material_" + texture.name;        // 生成材质的路径        string materialPath = AssetDatabase.GenerateUniqueAssetPath(System.IO.Path.Combine(assetFolderPath, materialName + ".mat"));        // 保存材质为 Asset        AssetDatabase.CreateAsset(mat, materialPath);        // 刷新 Asset 数据库        AssetDatabase.Refresh();        // 检查是否成功创建        Material createdMat = AssetDatabase.LoadAssetAtPath<Material>(materialPath);        if (createdMat == null)        {            Debug.LogError("Failed to create material at path: " + materialPath);        }        return createdMat;    }    private void InstantiateQuads(GameObject prefab, int hor, int ver, Transform parent)    {        //// 加载所有纹理        //Texture[] textures = Resources.LoadAll<Texture>("L14");        if (prefab == null)        {            Debug.LogError("Prefab is not assigned.");            return;        }        for (int i = 0; i < hor; i++)        {            for (int j = 0; j < ver; j++)            {                Texture texture = Resources.Load<Texture>("L13/GCJ02_L13_21x21" + i + "_" + j);                float startJ = 0 - (hor / 2.0f) + 0.5f;                float starti = 0 + (ver / 2.0f) - 0.5f;                // 实例化Prefab                GameObject quad = Instantiate(prefab, parent);                quad.name = "L13" + i.ToString() + "_" + j.ToString();                // 设置位置                Vector3 localPosition = new Vector3(startJ + j, starti - i, 0);                quad.transform.localPosition = localPosition;                // 为每个实例化的对象创建新的材质,并保存为Asset                Material mat = CreateAndSaveMaterial(texture, "Assets/Art/Material/Minimap/L13");                quad.GetComponent<MeshRenderer>().material = mat;            }        }    }}
 |