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/展示材質(zhì)球")]
private static void ShowWindow()
{
var window = GetWindow<MaterialShowEditor>();
window.titleContent = new GUIContent("展示材質(zhì)球");
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預(yù)覽", EditorStyles.whiteLargeLabel);
if (GUILayout.Button("創(chuàng)建一個(gè)預(yù)覽材質(zhì)球"))
{
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("保存材質(zhì)球到本地"))
{
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格式輸出材質(zhì)球參數(shù)"))
{
ExportMaterialPropertys();
}
GUILayout.EndVertical();
}
}
private void MaterialParamsGUI()
{
GUILayout.Label("Material參數(shù)", 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中展示材質(zhì)球(Material)并獲取參數(shù)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 今天給大家推薦一款神奇的材質(zhì)腳本,名字叫POLIIGON Material Convert,大家知道在魔頓網(wǎng)站上下...
- 材質(zhì)(Materials)用來把網(wǎng)格(Mesh)或粒子渲染器(Particle Renderers)貼到游戲?qū)ο笊?..
- 前言: 把物理世界的體驗(yàn)帶進(jìn)屏幕。去除現(xiàn)實(shí)中的雜志和隨機(jī)性,保留其最原始純凈的形態(tài)、空間關(guān)系、變化與過渡,配合虛擬...