Version: Unity 6.0 (6000.0)
语言 : 中文
使用材质属性值设置着色器变量
通过分支和关键字更改着色器的工作方式

在检视面板窗口中控制材质属性

在 Unity 编辑器中,可以控制材质属性在检视面板 (Inspector) 窗口中的显示方式。为此,最简单的方法是使用 MaterialPropertyDrawer

对于更复杂的需求,可以使用 MaterialEditorMaterialPropertyShaderGUI 类。

使用自定义编辑器

使用自定义编辑器可显示 Unity 无法使用其默认材质检视面板显示的数据类型,或定义自定义控件或数据验证。

__ ShaderLab__Unity 用于定义着色器对象结构的语言。更多信息
See in Glossary
中,可以为所有渲染管线指定一个自定义编辑器。为此,可将 CustomEditor 代码块放置在 Shader 代码块内。此外,还可以为基于可编程渲染管线的渲染管线指定不同的自定义编辑器,方法是在 Shader 代码块中放置一个 CustomEditorForRenderPipeline 代码块。如果您的代码同时包含 CustomEditorCustomEditorForRenderPipeline 代码块,则特定于渲染管线的代码块会覆盖 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.
        }
    }
}

其他资源

使用材质属性值设置着色器变量
通过分支和关键字更改着色器的工作方式
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961