Unity エディターでは、Inspector ウィンドウに表示されるマテリアルのプロパティを制御することができます。最も簡単な方法は MaterialPropertyDrawer を使うことです。
複雑なニーズには、クラス MaterialEditor、MaterialProperty、および ShaderGUI を使用できます。
カスタムエディターを使用して、Unity のデフォルトのマテリアルインスペクターでは表示できないデータタイプを表示したり、カスタムコントロールやデータ検証を定義します。
ShaderLab では、すべてのレンダーパイプラインにカスタムエディターを割り当てることができます。それには、CustomEditor ブロックを Shader ブロックの中に配置します。CustomEditorForRenderPipeline ブロックを Shader ブロックの中に配置することで、スクリプタブルレンダーパイプラインに基づいて、レンダーパイプラインに様々なカスタムエディターを割り当てることができます。コードに CustomEditor と CustomEditorForRenderPipeline の両方のブロックが含まれている場合は、レンダーパイプライン固有のブロックが CustomEditor ブロックをオーバーライドします。
特定のシェーダーオブジェクトを表すシェーダーアセットに対してカスタムエディターを定義するには、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 ブロックを使用して特定のレンダーパイプラインアセット用に 2 つの追加カスタムエディターを指定する構文を示しています。
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.
}
}
}