最近在写用Instancing方法做的云的时候发现在脚本上调材质很麻烦。
像这样要跳到材质才能调,于是我写了一个脚本:
using UnityEditor;
[CustomEditor(typeof(TestMaterial))]
public class TestMaterialEditor : Editor
{
TestMaterial draw;
MaterialEditor matEditor;
private void OnEnable()
{
draw = (TestMaterial)target;
}
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
base.OnInspectorGUI();
if (draw.material != null)
{
EditorGUILayout.Space();
if (EditorGUI.EndChangeCheck() || matEditor == null)
{
matEditor = CreateEditor(draw.material) as UnityEditor.MaterialEditor;
}
matEditor.DrawHeader();
if (matEditor.isVisible)
{
matEditor.PropertiesGUI();
}
matEditor.RegisterPropertyChangeUndo("Material");
}
}
}
于是材质面板出来了,可以直接调了: 多材质
using UnityEditor;
[CustomEditor(typeof(TestMaterial))]
public class TestMaterialEditor : Editor
{
TestMaterial draw;
MaterialEditor[] matEditor;
private void OnEnable()
{
draw = (TestMaterial)target;
}
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
base.OnInspectorGUI();
int count = draw.materials != null ? draw.materials.Length : 0;
if (count > 0)
{
EditorGUILayout.Space();
if (EditorGUI.EndChangeCheck() || matEditor == null)
{
matEditor = new MaterialEditor[count];
for (int i = 0; i < count; i++)
{
if (draw.materials[i] == null) continue;
matEditor[i] = CreateEditor(draw.materials[i]) as UnityEditor.MaterialEditor;
}
}
for (int i = 0; i < count; i++)
{
if (draw.materials[i] == null) continue;
matEditor[i].DrawHeader();
if (matEditor[i].isVisible)
{
matEditor[i].PropertiesGUI();
}
matEditor[i].RegisterPropertyChangeUndo("Material");
}
}
}
}
虽然用的地方不多,但还是记录一下。
|