Version: 2022.3

MaterialPropertyDrawer

class in UnityEditor

切换到手册

描述

用于派生自定义材质属性绘制器的基类。

使用此基类为您的材质属性创建自定义 UI 绘制器,无需编写自定义 MaterialEditor 类。这类似于 PropertyDrawer 启用自定义 UI,而不编写自定义检视面板。

在着色器代码中,可在着色器属性前面使用类似于 C# 的属性语法向其添加绘制器。Unity 拥有多个内置绘制器,您也可以编写自己的绘制器。下面是一个演示此语法的着色器代码片段:

          Shader "Custom/Example"
{
    Properties
    {
        _MainTex("Base (RGB)", 2D) = "white" {}

// Display a popup with None,Add,Multiply choices, // and setup corresponding shader keywords. [KeywordEnum(None, Add, Multiply)] _Overlay("Overlay mode", Float) = 0

_OverlayTex("Overlay", 2D) = "black" {}

// Display as a toggle. [Toggle] _Invert("Invert color?", Float) = 0 }

// rest of shader code... }

在实现您自己的绘制器时,您应该重写 OnGUI 函数。您还可以选择重写 GetPropertyHeight 和 Apply 函数。以下是属性绘制器示例,其中显示了与浮点属性对应的复选框,并根据状态将值设为 0 或 1:

using UnityEngine;
using UnityEditor;
using System;

// The property drawer class should be placed in an editor script, inside a folder called Editor. // Use with "[MyToggle]" before a float shader property.

public class MyToggleDrawer : MaterialPropertyDrawer { // Draw the property inside the given rect public override void OnGUI (Rect position, MaterialProperty prop, String label, MaterialEditor editor) { // Setup bool value = (prop.floatValue != 0.0f);

EditorGUI.BeginChangeCheck(); EditorGUI.showMixedValue = prop.hasMixedValue;

// Show the toggle control value = EditorGUI.Toggle(position, label, value);

EditorGUI.showMixedValue = false; if (EditorGUI.EndChangeCheck()) { // Set the new value if it has changed prop.floatValue = value ? 1.0f : 0.0f; } } }

The built-in MaterialPropertyDrawers are: ToggleDrawer, ToggleOffDrawer, KeywordEnumDrawer, EnumDrawer, PowerSliderDrawer, IntRangeDrawer. In shader code, the "Drawer" suffix of the class name is not written; when Unity searches for the drawer class it adds "Drawer" automatically.

Toggle allows you to enable or disable a single shader keyword. It uses a float to store the state of the shader keyword, and displays it as a toggle. When the toggle is enabled, Unity enables the shader keyword; when the toggle is disabled, Unity disables the shader keyword.

If you specify a keyword name, the toggle affects a shader keyword with that name. If you don't specify a shader keyword name, the toggle affects a shader keyword with the name (uppercase property name)_ON.

// This version specifies a keyword name.
// The material property name is not important.
// When the toggle is enabled, Unity enables a shader keyword with the name "ENABLE_EXAMPLE_FEATURE".
// When the toggle is disabled, Unity disables a shader keyword with the name "ENABLE_EXAMPLE_FEATURE".
[Toggle(ENABLE_EXAMPLE_FEATURE)] _ExampleFeatureEnabled ("Enable example feature", Float) = 0

// This version does not specify a keyword name. // The material property name determines the shader keyword it affects. // When this toggle is enabled, Unity enables a shader keyword with the name "_ANOTHER_FEATURE_ON". // When this toggle is disabled, Unity disables a shader keyword with the name "_ANOTHER_FEATURE_ON". [Toggle] _Another_Feature ("Enable another feature", Float) = 0

// ...Later, in the HLSL code: #pragma multi_compile __ ENABLE_EXAMPLE_FEATURE #pragma multi_compile __ _ANOTHER_FEATURE_ON

ToggleOff is similar to Toggle, but when the toggle is enabled, Unity disables the shader keyword; when the toggle is disabled, Unity enables the shader keyword. Additionally, the default name when you don't specify a shader keyword name is (uppercase property name)_OFF.

ToggleOff can be useful for adding functionality and toggles to existing shaders, while maintaining backwards compatibility.

// This version specifies a keyword name.
// The material property name is not important.
// When the toggle is enabled, Unity disables a shader keyword with the name "DISABLE_EXAMPLE_FEATURE".
// When the toggle is disabled, Unity enables a shader keyword with the name "DISABLE_EXAMPLE_FEATURE".
[ToggleOff(DISABLE_EXAMPLE_FEATURE)] _ExampleFeatureEnabled ("Enable example feature", Float) = 0

// This version does not specify a keyword name. // The material property name determines the shader keyword it affects. // When this toggle is enabled, Unity disables a shader keyword with the name "_ANOTHER_FEATURE_OFF". // When this toggle is disabled, Unity enables a shader keyword with the name "_ANOTHER_FEATURE_OFF". [ToggleOff] _Another_Feature ("Enable another feature", Float) = 0

// ...Later, in the HLSL code: #pragma multi_compile __ DISABLE_EXAMPLE_FEATURE #pragma multi_compile __ _ANOTHER_FEATURE_OFF

KeywordEnum allows you to choose which of a set of shader keywords to enable. It displays a float as a popup menu, and the value of the float determines which shader keyword Unity enables. Unity enables a shader keyword with the name (uppercase property name)_(uppercase enum value name). Up to 9 names can be provided.

// Display a popup with None, Add, Multiply choices.
// Each option will set _OVERLAY_NONE, _OVERLAY_ADD, _OVERLAY_MULTIPLY shader keywords.
[KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0

// ...Later, in the HLSL code: #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY

**Enum** 显示与浮点属性对应的弹出菜单。您可以提供枚举类型名称(最好完全限定命名空间,以防存在多种类型),或者要显示的显式名称/值对。最多可指定 7 个名称/值对。

// Blend mode values
[Enum(UnityEngine.Rendering.BlendMode)] _Blend ("Blend mode", Float) = 1

// A subset of blend mode values, just "One" (value 1) and "SrcAlpha" (value 5). [Enum(One,1,SrcAlpha,5)] _Blend2 ("Blend mode subset", Float) = 1

**PowerSlider** 显示一个具有对应于 Range 着色器属性的非线性响应的滑动条。

// 带有 3.0 响应曲线的滑动条
[PowerSlider(3.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.08

**IntRange** 显示一个适用于 Range 着色器属性的整数滑动条。

// 适用于指定范围的整数滑动条(0 到 255)
[IntRange] _Alpha ("Alpha", Range (0, 255)) = 100

当属性绘制器类名称以“Decorator”结尾时(即为属性装饰器),与 DecoratorDrawer 相似。它们用于在属性之间创建 不影响属性本身的标题和分隔栏。单个属性上可以有多个装饰器。内置的装饰器绘制器有:SpaceDecorator 和 HeaderDecorator。

**Space** 在着色器属性之前创建垂直空间。

// Default small amount of space.
[Space] _Prop1 ("Prop1", Float) = 0

// Large amount of space. [Space(50)] _Prop2 ("Prop2", Float) = 0

**Header** 在着色器属性前创建标题文本。

[Header(A group of things)] _Prop1 ("Prop1", Float) = 0

请注意,出于性能原因,EditorGUILayout 函数不能与 MaterialPropertyDrawers 一起使用。

另请参阅:MaterialProperty 类。

公共函数

Apply将额外的初始值应用于该材质。
GetPropertyHeight重写此方法,以指定此属性的 GUI 的高度(以像素为单位)。
OnGUI重写此方法,以针对该属性创建您自己的 GUI。