Unity Editor 为材质显示默认编辑面板


最近在写用Instancing方法做的云的时候发现在脚本上调材质很麻烦。

Unity Editor 为材质显示默认编辑面板_第1张图片
像这样要跳到材质才能调,于是我写了一个脚本:

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();
            //检查面板是否改变,改变了就重新生成MaterialEditor
            if (EditorGUI.EndChangeCheck() || matEditor == null) 
            {
                matEditor = CreateEditor(draw.material) as UnityEditor.MaterialEditor;
            }

            matEditor.DrawHeader();
            if (matEditor.isVisible)
            {
                matEditor.PropertiesGUI();
            }

            //加上这句,不然Ctrl+Z会卡顿
            matEditor.RegisterPropertyChangeUndo("Material");
        }
    }
}

于是材质面板出来了,可以直接调了:
Unity Editor 为材质显示默认编辑面板_第2张图片
多材质

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();
                }

                //加上这句,不然Ctrl+Z会卡顿
                matEditor[i].RegisterPropertyChangeUndo("Material");
            }
        }
    }
}

Unity Editor 为材质显示默认编辑面板_第3张图片
虽然用的地方不多,但还是记录一下。

你可能感兴趣的:(Unity,Editor,unity,Unity,Editor)