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<MaterialShowEditor>();
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<ShaderProperty> shaderProperties { get; set; }
}
private Vector2 materialScrollPos;
private MaterialEditor m_MaterialEditor;
string saveMaterialName = "test";
private Dictionary<string, ModelPart> modelPartDict = new Dictionary<string, ModelPart>();
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<ShaderProperty> shaderProperties = new List<ShaderProperty>();
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)并获取参数
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 今天给大家推荐一款神奇的材质脚本,名字叫POLIIGON Material Convert,大家知道在魔顿网站上下...
- 材质(Materials)用来把网格(Mesh)或粒子渲染器(Particle Renderers)贴到游戏对象上...
- 前言: 把物理世界的体验带进屏幕。去除现实中的杂志和随机性,保留其最原始纯净的形态、空间关系、变化与过渡,配合虚拟...