EditorWindow中展示材质球(Material)并获取参数

using ModelMaterialPreview;
using Newtonsoft.Json;
using Pathfinding;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using static Pathfinding.GridGraph;

public class MaterialShowEditor : EditorWindow
{
    [MenuItem("Window/展示材质球")]
    private static void ShowWindow()
    {
        var window = GetWindow();
        window.titleContent = new GUIContent("展示材质球");
        window.Show();
    }


    [Serializable]
    public class ShaderAB
    {
        public string asset { get; set; }
        public string bundle { get; set; }
    }
    [Serializable]
    public class ShaderProperty
    {
        public string shaderPropertyType { get; set; }
        public string name { get; set; }
        public object value { get; set; }
    }
    [Serializable]
    public class ModelPart
    {
        public ShaderAB shaderAB { get; set; }

        public List shaderProperties { get; set; }
    }


    private Vector2 materialScrollPos;
    private MaterialEditor m_MaterialEditor;
    string saveMaterialName = "test";
    private Dictionary modelPartDict = new Dictionary();


    private void OnGUI()
    {

        GUILayout.BeginHorizontal(GUILayout.Width(800));

        GUILayout.BeginVertical("HelpBox", GUILayout.Width(260));
        MaterialGUI();
        GUILayout.EndVertical();

        GUILayout.BeginVertical("HelpBox", GUILayout.Width(500));
        MaterialParamsGUI();
        GUILayout.EndVertical();

        GUILayout.EndHorizontal();

    }

    private void MaterialGUI()
    {
        GUILayout.Label("Material预览", EditorStyles.whiteLargeLabel);

        if (GUILayout.Button("创建一个预览材质球"))
        {
            Shader shader = Shader.Find("Standard");
            Material material = new Material(shader);
            m_MaterialEditor = (MaterialEditor)Editor.CreateEditor(material);
        }
        GUILayout.Space(20);

        if (m_MaterialEditor != null && m_MaterialEditor.target != null)
        {
            GUILayout.BeginVertical("HelpBox");
            GUIStyle bgColor = new GUIStyle();
            bgColor.normal.background = EditorGUIUtility.whiteTexture;
            m_MaterialEditor.DrawHeader();
            m_MaterialEditor.OnPreviewGUI(GUILayoutUtility.GetRect(256, 256), bgColor);

            GUILayout.Label(m_MaterialEditor.target.name, EditorStyles.whiteLargeLabel);

    
            GUILayout.Space(10);
            GUILayout.BeginHorizontal();
            GUILayout.Label("保存名:", GUILayout.Width(50));
            saveMaterialName = GUILayout.TextField(saveMaterialName);
            GUILayout.EndHorizontal();
            if (GUILayout.Button("保存材质球到本地"))
            {
                string path = EditorUtility.OpenFolderPanel("选择路径", "", "");
                if (!string.IsNullOrEmpty(path))
                {
                    Material material = (Material)m_MaterialEditor.target;
                    Material material_copy = new Material(material.shader);
                    material_copy.CopyPropertiesFromMaterial(material);
                    AssetDatabase.CreateAsset(material_copy, $"{path}\\{saveMaterialName}.mat");
                    AssetDatabase.Refresh();
                }
            }
            GUILayout.Space(10);
            if (GUILayout.Button("Json格式输出材质球参数"))
            {
                ExportMaterialPropertys();
            }
            GUILayout.EndVertical();
        }
    }

    private void MaterialParamsGUI()
    {
        GUILayout.Label("Material参数", EditorStyles.whiteLargeLabel);

        if (m_MaterialEditor != null && m_MaterialEditor.target != null)
        {
            materialScrollPos = EditorGUILayout.BeginScrollView(materialScrollPos);
            GUILayout.BeginVertical("HelpBox");
            m_MaterialEditor.OnInspectorGUI();
            GUILayout.EndVertical();
            EditorGUILayout.EndScrollView();
        }
    }


    private void ExportMaterialPropertys()
    {
        modelPartDict.Clear();

        ModelPart modelPart = new ModelPart();
        List shaderProperties = new List();

        Material material = (Material)m_MaterialEditor.target;
        int propertyCount = ShaderUtil.GetPropertyCount(material.shader);
        for (int i = 0; i < propertyCount; i++)
        {
            ShaderProperty shaderProperty = GetShaderProperty(material, i);
            shaderProperties.Add(shaderProperty);
        }
        modelPart.shaderProperties = shaderProperties;

        //string shaderPath = AssetDatabase.GetAssetPath(material.shader);

        //AssetDatabase.Refresh();
        //AssetDatabase.ImportAsset(shaderPath, ImportAssetOptions.ForceUpdate);

        //AssetImporter importer = AssetImporter.GetAtPath(shaderPath);

        //ShaderAB shaderAB = new ShaderAB();
        //shaderAB.asset = System.IO.Path.GetFileNameWithoutExtension(shaderPath).ToLower();
        //shaderAB.bundle = importer.assetBundleName;
        //Debug.Log(shaderAB.asset);
        //Debug.Log(shaderAB.bundle);
        //modelPart.shaderAB = shaderAB;

        modelPartDict.Add("test1", modelPart);

        string jsonStr = JsonConvert.SerializeObject(modelPartDict);
        Debug.Log(jsonStr);
    }

    private ShaderProperty GetShaderProperty(Material material, int index)
    {

        string propName = ShaderUtil.GetPropertyName(material.shader, index);
        string propDescription = ShaderUtil.GetPropertyDescription(material.shader, index);
        ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetPropertyType(material.shader, index);

        ShaderProperty shaderProperty = new ShaderProperty();
        shaderProperty.shaderPropertyType = propertyType.ToString();
        shaderProperty.name = propName;

        switch (propertyType)
        {
            case ShaderUtil.ShaderPropertyType.Color:
                Color color = material.GetColor(propName);
                shaderProperty.value = $"{{r:{color.r},b:{color.b},g:{color.g},a:{color.a}}}";
                break;
            case ShaderUtil.ShaderPropertyType.Vector:
                var vector = material.GetVector(propName);
                shaderProperty.value = $"{{x:{vector.x},y:{vector.y},z:{vector.z},w:{vector.w}}}";
                break;
            case ShaderUtil.ShaderPropertyType.Float:
                var fValue = material.GetFloat(propName);
                shaderProperty.value = fValue;
                break;
            case ShaderUtil.ShaderPropertyType.Range:
                var rValue = material.GetFloat(propName);
                shaderProperty.value = rValue;
                break;
            case ShaderUtil.ShaderPropertyType.TexEnv:
                Texture tex = material.GetTexture(propName);
                if (tex)
                {
                    string texturePath = AssetDatabase.GetAssetPath(tex);
                    AssetImporter importer = AssetImporter.GetAtPath(texturePath);
                    shaderProperty.value = $"{{asset:'{System.IO.Path.GetFileNameWithoutExtension(texturePath).ToLower()}',bundle:'{importer.assetBundleName}'}}";
                }
                else
                {
                    shaderProperty.value = "{asset:'',bundle:''}";
                }
                break;
            case ShaderUtil.ShaderPropertyType.Int:
                var iValue = material.GetInt(propName);
                shaderProperty.value = iValue;
                break;
        }

        return shaderProperty;
    }

}

你可能感兴趣的:(EditorWindow中展示材质球(Material)并获取参数)