Version: 2018.1

MaterialPropertyDrawer

class in UnityEditor

マニュアルに切り替える

説明

プロパティー Drawer の派生元ベースクラス

これを使用して自身のマテリアルプロパティーのためのカスタム UI Drawer を、カスタムの MaterialEditor クラスを記述することなく、作成できます。このクラスは PropertyDrawer がカスタムのインスペクターを記述することなくカスタム UI を可能にする方法に似ています。

シェーダーコードでは C# のような属性の文法を、シェーダープロパティーの前に使用して Drawer を追加できます。Unity にはいくつかのビルトインの Drawer があり、自身で記述することもできます。次にシンタックスを示すシェーダーコードのスニペットを示します。

          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... }

When implementing your own drawers, you should override OnGUI function. You can also optionally override GetPropertyHeight and Apply functions. Here's an example of a property drawer that displays a checkbox for a float property, with the value set to 0 or 1 depending on the state:

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, EnumDrawer, KeywordEnumDrawer, 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 により float をトグルとして表示します。プロパティーの値はトグルの状態により 0 か 1 となります。有効であるときシェーダーキーワードで大文字のプロパティー名 + "_ ON"、または明示的に指定されたシェーダーキーワードがセットされます。

// Will set "_INVERT_ON" shader keyword when set
[Toggle] _Invert ("Invert?", Float) = 0

// Will set "ENABLE_FANCY" shader keyword when set. [Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0

Enum により float プロパティーにポップアップメニューを表示します。列挙体の型名(望ましいのは、複数の型がある場合を想定して完全修飾された名前空間)、明示的な名前 / 値のペアを指定できます。最大で 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

KeywordEnum により float プロパティーにポップアップメニューを表示して、対応するシェーダーキーワードを有効化します。これはシェーダーにおいて、シェーダーコードの一部を有効化 / 無効化するために "#pragma multi_ compile" とともに使用されます。 おのおのの名前は大文字で "プロパティー名" + "_" + "列挙体名" のシェーダーキーワードを有効化します。最大で 9 つの名前を指定できます。

// 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 on in CGPROGRAM code: #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY // ...

PowerSlider により Range シェーダープロパティーに対する線形でない反応曲線のスライダーを表示します。

// A slider with 3.0 response curve
[PowerSlider(3.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.08

IntRange displays an integer slider for a Range shader property.

// An integer slider for specified range (0 to 255)
[IntRange] _Alpha ("Alpha", Range (0, 255)) = 100

When a property drawer class name ends with "Decorator", that is a property decorator, similar to DecoratorDrawer. They are used to create headings and dividers between properties that don't affect the property itself. A single property can have multiple decorators on it. The built-in decorator drawers are: 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 関数は MaterialPropertyDrawer では使用できません。

関連項目: MaterialProperty クラス

Public 関数

Applyマテリアルに追加の初期値を適用します
GetPropertyHeightGUI の高さを指定するにはこのメソッドのプロパティーをピクセル単位でオーバーライドします
OnGUIこのメソッドをオーバーライドしてプロパティーに自身の GUI を作成します
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961