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

public class Editor_Tool : EditorWindow
{
    //增加菜单栏选项
    [MenuItem("modification/修改白色材质")]
    public static void ChangeCorss()
    {

        //获取编辑器中当前选中的物体
        Transform[] objs = Selection.transforms;
        //如果没有选择任何物体，弹出提示并退出
        Debug.Log(objs.Length);
        if (objs.Length == 0)
        {
            Debug.Log("没有选中物体");
            return;
        }
        var path = "Assets/Prefabs/M/whitemat.mat";
        ForeachObjCorss(objs, path);
    }
    [MenuItem("modification/修改紫色材质")]
    public static void Changeperple()
    {

        //获取编辑器中当前选中的物体
        Transform[] objs = Selection.transforms;
        //如果没有选择任何物体，弹出提示并退出
        Debug.Log(objs.Length);
        if (objs.Length == 0)
        {
            Debug.Log("没有选中物体");
            return;
        }
        var path = "Assets/Prefabs/M/perple.mat";
        ForeachObjCorss(objs, path);
    }

    [MenuItem("modification/修改红色材质")]
    public static void ChangeRed()
    {

        //获取编辑器中当前选中的物体
        Transform[] objs = Selection.transforms;
        //如果没有选择任何物体，弹出提示并退出
        Debug.Log(objs.Length);
        if (objs.Length == 0)
        {
            Debug.Log("没有选中物体");
            return;
        }
        var path = "Assets/Prefabs/M/redmat.mat";
        ForeachObjCorss(objs, path);
    }
    [MenuItem("modification/修改黑色材质")]
    public static void ChangeBlack()
    {

        //获取编辑器中当前选中的物体
        Transform[] objs = Selection.transforms;
        //如果没有选择任何物体，弹出提示并退出
        Debug.Log(objs.Length);
        if (objs.Length == 0)
        {
            Debug.Log("没有选中物体");
            return;
        }
        var path = "Assets/Prefabs/M/blackmat.mat";
        ForeachObjCorss(objs, path);
    }

    [MenuItem("modification/修改黄色材质")]
    public static void Changehuangse()
    {

        //获取编辑器中当前选中的物体
        Transform[] objs = Selection.transforms;
        //如果没有选择任何物体，弹出提示并退出
        Debug.Log(objs.Length);
        if (objs.Length == 0)
        {
            Debug.Log("没有选中物体");
            return;
        }
        var path = "Assets/Prefabs/M/yellowmat.mat";
        ForeachObjCorss(objs, path);
    }
    static void ForeachObjCorss(Transform[] obj, string path)
    {
        for (int i = 0; i < obj.Length; i++)
        {
            foreach (Renderer it in obj[i].GetComponentsInChildren<Renderer>())
            {
                Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);
                Material[] bufMat = new Material[it.sharedMaterials.Length];
                for (int j = 0; j < it.sharedMaterials.Length; j++)
                {
                    bufMat[j] = mat;
                }
                it.sharedMaterials = bufMat;
            }
        }
      
    }
}
