using UnityEngine; using UnityEditor;
// https://catlikecoding.com/unity/tutorials/rendering/part-9/
public class CustomShaderGUI : ShaderGUI { Material target; MaterialEditor editor; MaterialProperty[] properties;
public override void OnGUI(MaterialEditor editor, MaterialProperty[] properties) { this.target = editor.target as Material; this.editor = editor; this.properties = properties; Main(); }
MaterialProperty FindProperty(string name) { return FindProperty(name, properties); }
static GUIContent staticLabel = new GUIContent();
static GUIContent MakeLabel(MaterialProperty property, string tooltip = null) { staticLabel.text = property.displayName; staticLabel.tooltip = tooltip; return staticLabel;
// it gets from shader properties name to custom editor }
void SetKeyword(string keyword, bool state) {
if (state) { target.EnableKeyword(keyword); } else { target.DisableKeyword(keyword); }
}
void Main( ) { GUILayout.Space(20); GUILayout.Label("Custom GUI Sample Code shader", EditorStyles.boldLabel);
GUILayout.Space(10); GUILayout.Label("Legacy Lambert Shader", EditorStyles.label); GUILayout.Space(10);
//MaterialProperty tint = FindProperty("_TintColor", properties); MaterialProperty mainTex = FindProperty("_MainTex", properties);
GUIContent albedoLabel = new GUIContent(mainTex.displayName, "Albedo(RGB)"); //editor.TexturePropertySingleLine(albedoLabel, mainTex, tint); //editor.TexturePropertySingleLine(albedoLabel, mainTex, FindProperty("_TintColor")); editor.TexturePropertySingleLine(MakeLabel(mainTex, "Albedo(RGB)"), mainTex, FindProperty("_TintColor"));
Normal( ); Brightness( ); editor.TextureScaleOffsetProperty(mainTex); Emissive( ); }
void Normal( ) { MaterialProperty map = FindProperty("_NormalMap"); //editor.TexturePropertySingleLine(MakeLabel(map, "Normal Texture"), map, FindProperty("_BumpScale")); //Hidden Normal Intensity value when texture does not has. editor.TexturePropertySingleLine(MakeLabel(map, "Normal Texture"), map, map.textureValue ? FindProperty("_BumpScale") : null); //offsite code //editor.TexturePropertySingleLine(MakeLabel(map, "Normal Texture"), map, map.textureValue ? null : FindProperty("_BumpScale")); }
void Brightness( ) { MaterialProperty bright = FindProperty("_Brightness"); EditorGUI.indentLevel += 2; //editor.ShaderProperty(bright,"Brightness"); editor.ShaderProperty(bright, MakeLabel(bright, "Brightness value control")); EditorGUI.indentLevel -= 2; }
void Emissive( ) { MaterialProperty map = FindProperty("_EmiTex"); EditorGUI.BeginChangeCheck(); editor.TexturePropertySingleLine(MakeLabel(map,"Emissive texture"), map, map.textureValue ? FindProperty("_EmiInten") : null);
if (EditorGUI.EndChangeCheck()) { SetKeyword("EMISSION_ON", map.textureValue); } }
}
|