using System.IO;
using UnityEditor;
using UnityEngine;

namespace NBF
{
    public static class TestEditorUtil
    {

        [MenuItem("Assets/修改替换材质", false, 1)]
        private static void ReplaceShadersInSelectedFolder()
        {
            // 获取选中的材质文件
            Material[] selectedMaterials = Selection.GetFiltered<Material>(SelectionMode.Assets);

            if (selectedMaterials.Length == 0)
            {
                Debug.LogWarning("No materials selected. Please select materials in the Project window.");
                return;
            }

            int replacedCount = 0;

            foreach (Material material in selectedMaterials)
            {
                if (material.shader.name == "Standard")
                {
                    // 保存Albedo贴图
                    Texture albedoTexture = material.GetTexture("_MainTex");

                    // 替换Shader为Universal Render Pipeline/Lit
                    material.shader = Shader.Find("Universal Render Pipeline/Lit");

                    // 将保存的Albedo贴图赋值给Base Map
                    if (albedoTexture != null)
                    {
                        material.SetTexture("_BaseMap", albedoTexture);
                    }

                    // 保存材质
                    EditorUtility.SetDirty(material);
                    replacedCount++;
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            Debug.Log($"Replaced {replacedCount} materials with URP Lit shader.");
        }
    }
}