using UnityEngine; using UnityEditor; using System; using UnityEngine.Rendering; #if UNITY_EDITOR public class BrewMonsterUnlitShaderGUI : ShaderGUI { // Properties private MaterialProperty colorProp; private MaterialProperty baseMapProp; private MaterialProperty shadowStrengthProp; private MaterialProperty alphaClipProp; private MaterialProperty cutoffProp; private MaterialProperty alphaModulateProp; private MaterialProperty surfaceProp; private MaterialProperty srcBlendProp; private MaterialProperty dstBlendProp; private MaterialProperty zWriteProp; private MaterialProperty cullProp; // UI Labels private static readonly GUIContent baseMapLabel = new GUIContent("Base Map"); private static readonly GUIContent colorLabel = new GUIContent("Base Color"); private static readonly GUIContent shadowStrengthLabel = new GUIContent("Shadow Strength"); private static readonly GUIContent surfaceOptionsLabel = new GUIContent("Surface Options"); private static readonly GUIContent alphaOptionsLabel = new GUIContent("Alpha Options"); private static readonly string[] surfaceNames = Enum.GetNames(typeof(SurfaceType)); // Enums private enum SurfaceType { Opaque, Transparent } public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties) { // Find properties colorProp = FindProperty("_Color", properties); baseMapProp = FindProperty("_BaseMap", properties); shadowStrengthProp = FindProperty("_ShadowStrength", properties); alphaClipProp = FindProperty("_AlphaClip", properties); cutoffProp = FindProperty("_Cutoff", properties); alphaModulateProp = FindProperty("_AlphaModulate", properties); surfaceProp = FindProperty("_Surface", properties); srcBlendProp = FindProperty("_SrcBlend", properties); dstBlendProp = FindProperty("_DstBlend", properties); zWriteProp = FindProperty("_ZWrite", properties); cullProp = FindProperty("_Cull", properties); Material material = materialEditor.target as Material; // Draw UI EditorGUILayout.Space(); EditorGUILayout.LabelField(surfaceOptionsLabel, EditorStyles.boldLabel); DoSurfaceTypePopup(material); materialEditor.ShaderProperty(cullProp, "Cull Mode"); EditorGUILayout.Space(); materialEditor.TexturePropertySingleLine(baseMapLabel, baseMapProp, colorProp); materialEditor.ShaderProperty(shadowStrengthProp, shadowStrengthLabel); EditorGUILayout.Space(); EditorGUILayout.LabelField(alphaOptionsLabel, EditorStyles.boldLabel); materialEditor.ShaderProperty(alphaClipProp, "Alpha Clipping"); if (material.HasProperty("_AlphaClip") && material.GetFloat("_AlphaClip") > 0) { materialEditor.ShaderProperty(cutoffProp, "Alpha Cutoff", 1); } SurfaceType surfaceType = (SurfaceType)material.GetFloat("_Surface"); if (surfaceType == SurfaceType.Transparent) { materialEditor.ShaderProperty(alphaModulateProp, "Multiply Color by Alpha"); } EditorGUILayout.Space(); materialEditor.RenderQueueField(); materialEditor.EnableInstancingField(); materialEditor.DoubleSidedGIField(); } private void DoSurfaceTypePopup(Material material) { EditorGUI.BeginChangeCheck(); SurfaceType surfaceType = (SurfaceType)material.GetFloat("_Surface"); var selectedSurfaceType = (SurfaceType)EditorGUILayout.Popup("Surface Type", (int)surfaceType, surfaceNames); if (EditorGUI.EndChangeCheck()) { material.SetFloat("_Surface", (float)selectedSurfaceType); SetupMaterialBlendMode(material, selectedSurfaceType); } } private void SetupMaterialBlendMode(Material material, SurfaceType surfaceType) { switch (surfaceType) { case SurfaceType.Opaque: material.SetOverrideTag("RenderType", "Opaque"); material.SetInt("_SrcBlend", (int)BlendMode.One); material.SetInt("_DstBlend", (int)BlendMode.Zero); material.SetInt("_ZWrite", 1); material.renderQueue = (int)RenderQueue.Geometry; material.DisableKeyword("_ALPHAMODULATE_ON"); break; case SurfaceType.Transparent: material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.renderQueue = (int)RenderQueue.Transparent; break; } } } #endif