在 Unity 编辑器中,可以控制材质属性在检视面板 (Inspector) 窗口中的显示方式。为此,最简单的方法是使用 MaterialPropertyDrawer。
对于更复杂的需求,可以使用 MaterialEditor、MaterialProperty 和 ShaderGUI 类。
使用自定义编辑器可显示 Unity 无法使用其默认材质检视面板显示的数据类型,或定义自定义控件或数据验证。
在__ ShaderLab__Unity 用于定义着色器对象结构的语言。更多信息
See in Glossary 中,可以为所有渲染管线指定一个自定义编辑器。为此,可将 CustomEditor 代码块放置在 Shader 代码块内。此外,还可以为基于可编程渲染管线的渲染管线指定不同的自定义编辑器,方法是在 Shader 代码块中放置一个 CustomEditorForRenderPipeline 代码块。如果您的代码同时包含 CustomEditor 和 CustomEditorForRenderPipeline 代码块,则特定于渲染管线的代码块会覆盖 CustomEditor 代码块。
要为代表给定 Shader 对象的着色器资源定义一个自定义编辑器,需要创建一个继承自 ShaderGUI 类的脚本。在 Assets 文件夹下,将脚本放在名为 Editor 的文件夹中。
脚本应遵循以下格式:
using UnityEditor;
public class ExampleShaderGUI : ShaderGUI
{
public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties)
{
// Custom code that controls the appearance of the Inspector goes here
base.OnGUI (materialEditor, properties);
}
}
此示例代码演示了为使用 CustomEditor 代码块的着色器资源指定默认自定义编辑器的语法,以及为使用 CustomEditorForRenderPipeline 代码块的特定渲染管线资源指定两个额外自定义编辑器的语法。
Shader "Examples/UsesCustomEditor"
{
// The Unity Editor uses the class ExampleCustomEditor to configure the Inspector for this shader asset
CustomEditor "ExampleShaderGUI"
CustomEditorForRenderPipeline "ExampleRenderPipelineShaderGUI" "ExampleRenderPipelineAsset"
CustomEditorForRenderPipeline "OtherExampleRenderPipelineShaderGUI" "OtherExampleRenderPipelineAsset"
SubShader
{
// Code that defines the SubShader goes here.
Pass
{
// Code that defines the Pass goes here.
}
}
}