Sometimes you have a shaderA small script that contains the mathematical calculations and algorithms for calculating the Color of each pixel rendered, based on the lighting input and the Material configuration. More info
See in Glossary with some interesting data types that can not be nicely represented using the built in Unity material editor. Unity provides a way to override the default way shader properties are presented so that you can define your own. You can use this feature to define custom controls and data range validation.
The first part to writing custom editor for your shader’s gui is defining a shader that requires a Custom Editor. The name you use for the custom editor is the class that will be looked up by Unity for the material editor.
To define a custom editor you extend from the ShaderGUI class and place the script below an Editor folder in the assetsAny media or data that can be used in your game or Project. An asset may come from a file created outside of Unity, such as a 3D model, an audio file or an image. You can also create some asset types in Unity, such as an Animator Controller, an Audio Mixer or a Render Texture. More info
See in Glossary directory.
using UnityEditor;
public class CustomShaderGUI : ShaderGUI
{
public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] properties)
{
base.OnGUI (materialEditor, properties);
}
}
Any shader that has a custom editor defined (CustomEditor “CustomShaderGUI”) will instantiate an instance of the shader gui class listed above and execute the associated code.
So we have a situation where we have a shader that can work in two modes; it renders standard diffuse lighting or it renders the blue and green channels with 50%.
Shader "Custom/Redify" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
__SubShader__Each shader in Unity consists of a list of subshaders. When Unity has to display a mesh, it will find the shader to use, and pick the first subshader that runs on the user's graphics card. [More info](SL-SubShader.html)<span class="tooltipGlossaryLink">See in [Glossary](Glossary.html#subshader)</span> {
Tags { "RenderType"="Opaque" }
__LOD__A system for building multiplayer capabilities for Unity games. It is built on top of the lower level transport real-time communication layer, and handles many of the common tasks that are required for multiplayer games. [More info](UNetUsingHLAPI.html)<span class="tooltipGlossaryLink">See in [Glossary](Glossary.html#LOD)</span> 200
CGPROGRAM
#pragma surface surf Lambert addshadow
#pragma shader_feature REDIFY_ON
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
#if REDIFY_ON
o.Albedo.gb *= 0.5;
#endif
}
ENDCG
}
CustomEditor "CustomShaderGUI"
}
As you can see the shader has a Keyword available for setting: REDIFY_ON. This can be changed be set on a per material basis by using the shaderKeywords property of the material. Below is an ShaderGUI instance that does this.
using UnityEngine;
using UnityEditor;
using System;
public class CustomShaderGUI : ShaderGUI
{
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
// render the default gui
base.OnGUI(materialEditor, properties);
Material targetMat = materialEditor.target as Material;
// see if redify is set, and show a checkbox
bool redify = Array.IndexOf(targetMat.shaderKeywords, "REDIFY_ON") != -1;
EditorGUI.BeginChangeCheck();
redify = EditorGUILayout.Toggle("Redify material", redify);
if (EditorGUI.EndChangeCheck())
{
// enable or disable the keyword based on checkbox
if (redify)
targetMat.EnableKeyword("REDIFY_ON");
else
targetMat.DisableKeyword("REDIFY_ON");
}
}
}
For a more comprehensive ShaderGUI example see the StandardShaderGUI.cs file together with the Standard.shader found in the ‘Built-in shaders’ package that can be downloaded from Unity Download Archive.
Note that the simple example above could also be solved much simpler using MaterialPropertyDrawers. Add the following line to the Properties section of the Custom/Redify shader:
[Toggle(REDIFY_ON)] _Redify("Red?", Int) = 0
and remove the:
CustomEditor "CustomShaderGUI"
Also see: MaterialPropertyDrawer
ShaderGUI should be used for more complex shader gui solutions where where e.g. material properties have dependencies
See in Glossary on each other or special layout is wanted. You can combine using MaterialPropertyDrawers with ShaderGUI classes, see StandardShaderGUI.cs.