//static bool disableOutlinePass = false;
    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
    {
        XSStyles.setupIcons();
        Material material = materialEditor.target as Material;

        {
            //Find all the properties within the shader
            shadowRamp           = ShaderGUI.FindProperty("_ShadowRamp", props);
            specMap              = ShaderGUI.FindProperty("_SpecularMap", props);
            specPattern          = ShaderGUI.FindProperty("_SpecularPattern", props);
            tint                 = ShaderGUI.FindProperty("_Color", props);
            mainTex              = ShaderGUI.FindProperty("_MainTex", props);
            normal               = ShaderGUI.FindProperty("_Normal", props);
            specIntensity        = ShaderGUI.FindProperty("_SpecularIntensity", props);
            specArea             = ShaderGUI.FindProperty("_SpecularArea", props);
            rimWidth             = ShaderGUI.FindProperty("_RimWidth", props);
            rimIntensity         = ShaderGUI.FindProperty("_RimIntensity", props);
            emissiveToggle       = ShaderGUI.FindProperty("_Emissive", props);
            emissiveTex          = ShaderGUI.FindProperty("_EmissiveTex", props);
            emissiveColor        = ShaderGUI.FindProperty("_EmissiveColor", props);
            alphaCutoff          = ShaderGUI.FindProperty("_Cutoff", props);
            culling              = ShaderGUI.FindProperty("_Culling", props);
            rimStyle             = ShaderGUI.FindProperty("_RimlightType", props);
            advMode              = ShaderGUI.FindProperty("_advMode", props);
            reflSmooth           = ShaderGUI.FindProperty("_ReflSmoothness", props);
            metal                = ShaderGUI.FindProperty("_Metallic", props);
            metalMap             = ShaderGUI.FindProperty("_MetallicMap", props);
            roughMap             = ShaderGUI.FindProperty("_RoughMap", props);
            bakedCube            = ShaderGUI.FindProperty("_BakedCube", props);
            shadowType           = ShaderGUI.FindProperty("_ShadowType", props);
            reflType             = ShaderGUI.FindProperty("_ReflType", props);
            saturation           = ShaderGUI.FindProperty("_Saturation", props);
            useRefl              = ShaderGUI.FindProperty("_UseReflections", props);
            matcapStyle          = ShaderGUI.FindProperty("_MatcapStyle", props);
            stylizedType         = ShaderGUI.FindProperty("_StylizedReflStyle", props);
            rampColor            = ShaderGUI.FindProperty("_RampColor", props);
            rimColor             = ShaderGUI.FindProperty("_RimColor", props);
            aX                   = ShaderGUI.FindProperty("_anistropicAX", props);
            aY                   = ShaderGUI.FindProperty("_anistropicAY", props);
            specStyle            = ShaderGUI.FindProperty("_SpecularStyle", props);
            detailNormal         = ShaderGUI.FindProperty("_DetailNormal", props);
            detailMask           = ShaderGUI.FindProperty("_DetailMask", props);
            normalStrength       = ShaderGUI.FindProperty("_NormalStrength", props);
            detailNormalStrength = ShaderGUI.FindProperty("_DetailNormalStrength", props);
            occlusionMap         = ShaderGUI.FindProperty("_OcclusionMap", props);
            occlusionStrength    = ShaderGUI.FindProperty("_OcclusionStrength", props);
            ThicknessMap         = ShaderGUI.FindProperty("_ThicknessMap", props);
            SSSDist              = ShaderGUI.FindProperty("_SSSDist", props);
            SSSPow               = ShaderGUI.FindProperty("_SSSPow", props);
            SSSIntensity         = ShaderGUI.FindProperty("_SSSIntensity", props);
            SSSCol               = ShaderGUI.FindProperty("_SSSCol", props);
            invertThickness      = ShaderGUI.FindProperty("_invertThickness", props);
            ThicknessMapPower    = ShaderGUI.FindProperty("_ThicknessMapPower", props);
            UseSSS               = ShaderGUI.FindProperty("_UseSSS", props);
            UseSpecular          = ShaderGUI.FindProperty("_UseSpecular", props);
            UseUV2Emiss          = ShaderGUI.FindProperty("_EmissUv2", props);
            EmissScaleWithLight  = ShaderGUI.FindProperty("_ScaleWithLight", props);
            EmissTintToColor     = ShaderGUI.FindProperty("_EmissTintToColor", props);
            EmissionPower        = ShaderGUI.FindProperty("_EmissionPower", props);
            if (material.shader.name.Contains("Outlined"))      //Shader.Find("Xiexe/Toon/XSToonCutoutOutlined") || material.shader == Shader.Find("Xiexe/Toon/XSToonOutlined") || material.shader == Shader.Find("Xiexe/Toon/XSToonTransparentDitheredOUTLINED"))
            {
                OutlineColor      = ShaderGUI.FindProperty("_OutlineColor", props);
                OutlineThickness  = ShaderGUI.FindProperty("_OutlineThickness", props);
                OutlineTextureMap = ShaderGUI.FindProperty("_OutlineTextureMap", props);
                _LitOutline       = ShaderGUI.FindProperty("_LitOutlines", props);
                outlined          = true;
            }
            else
            {
                outlined = false;
                outlines = false;
            }

            _AORAMPMODE_ON  = ShaderGUI.FindProperty("_AORAMPMODE_ON", props);
            _OcclusionColor = ShaderGUI.FindProperty("_OcclusionColor", props);

            _DetailNormalUv2    = ShaderGUI.FindProperty("_DetailNormalUv2", props);
            _NormalUv2          = ShaderGUI.FindProperty("_NormalUv2", props);
            _MetallicUv2        = ShaderGUI.FindProperty("_MetallicUv2", props);
            _SpecularUv2        = ShaderGUI.FindProperty("_SpecularUv2", props);
            _SpecularPatternUv2 = ShaderGUI.FindProperty("_SpecularPatternUv2", props);
            _AOUV2 = ShaderGUI.FindProperty("_AOUV2", props);

            //advanced options
            colorMask    = ShaderGUI.FindProperty("_colormask", props);
            stencil      = ShaderGUI.FindProperty("_Stencil", props);
            stencilComp  = ShaderGUI.FindProperty("_StencilComp", props);
            stencilOp    = ShaderGUI.FindProperty("_StencilOp", props);
            stencilFail  = ShaderGUI.FindProperty("_StencilFail", props);
            stencilZFail = ShaderGUI.FindProperty("_StencilZFail", props);
            zwrite       = ShaderGUI.FindProperty("_ZWrite", props);
            ztest        = ShaderGUI.FindProperty("_ZTest", props);

            RampBaseAnchor = ShaderGUI.FindProperty("_RampBaseAnchor", props);
            //
            //Show Properties in Inspector
            //materialEditor.ShaderProperty(, .displayName);

            EditorGUI.BeginChangeCheck();
            {
                EditorGUI.BeginChangeCheck();

                EditorGUI.showMixedValue = advMode.hasMixedValue;
                var aMode = (DisplayType)advMode.floatValue;

                EditorGUI.BeginChangeCheck();
                aMode = (DisplayType)EditorGUILayout.Popup("Shader Mode", (int)aMode, Enum.GetNames(typeof(DisplayType)));

                if (EditorGUI.EndChangeCheck())
                {
                    materialEditor.RegisterPropertyChangeUndo("Shader Mode");
                    advMode.floatValue       = (float)aMode;
                    EditorGUI.showMixedValue = false;
                }

                materialEditor.ShaderProperty(culling, culling.displayName);

                //main
                //Rect rect = (0,0);
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                materialEditor.TexturePropertySingleLine(Styles.MainTexText, mainTex, tint);
                XSStyles.helpPopup(XSStyles.mainURL);
                EditorGUILayout.EndHorizontal();
                GUI.skin = null;
                materialEditor.ShaderProperty(saturation, Styles.Saturation, 3);
                materialEditor.TextureScaleOffsetProperty(mainTex);

                //cutoff
                if (material.shader.name.Contains("Cutout") && !material.shader.name.Contains("AlphaToMask"))
                {
                    materialEditor.ShaderProperty(alphaCutoff, Styles.cutoutText);
                }
                if (material.shader.name.Contains("AlphaToMask"))
                {
                    //So that a fallback to standard works as intended.
                    material.SetFloat("_Cutoff", 0.5f);
                }
                //-----

                //outlines
                if (outlined == true)
                {
                    XSStyles.Separator();
                    EditorGUILayout.BeginHorizontal();
                    outlines = EditorGUILayout.Foldout(outlines, "OUTLINES", true);
                    XSStyles.helpPopup(XSStyles.outlineURL);
                    EditorGUILayout.EndHorizontal();
                    GUI.skin = null;
                    if (outlines)
                    {
                        XSStyles.SeparatorThin();
                        materialEditor.ShaderProperty(_LitOutline, "Outline Light Mode");
                        materialEditor.TexturePropertySingleLine(Styles.outlineTex, OutlineTextureMap);
                        materialEditor.ShaderProperty(OutlineColor, "Outline Color");
                        materialEditor.ShaderProperty(OutlineThickness, "Outline Scale");
                    }
                }
                //-----

                //normal map
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                normals = EditorGUILayout.Foldout(normals, "NORMAL MAPS", true);
                XSStyles.helpPopup(XSStyles.normalsURL);
                EditorGUILayout.EndHorizontal();
                GUI.skin = null;
                if (normals)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.TexturePropertySingleLine(Styles.normalText, normal, normalStrength);
                    materialEditor.TextureScaleOffsetProperty(normal);
                    materialEditor.TexturePropertySingleLine(Styles.detailNormal, detailNormal, detailNormalStrength);
                    materialEditor.TextureScaleOffsetProperty(detailNormal);
                    materialEditor.TexturePropertySingleLine(Styles.detailMask, detailMask);
                    materialEditor.ShaderProperty(_NormalUv2, "Normal UV");
                    materialEditor.ShaderProperty(_DetailNormalUv2, "Detail UV");
                }
                //-----

                //shadows
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                shadows = EditorGUILayout.Foldout(shadows, "SHADOWS", true);
                XSStyles.helpPopup(XSStyles.shadowsURL);
                EditorGUILayout.EndHorizontal();
                GUI.skin = null;
                if (shadows)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.TexturePropertySingleLine(Styles.rampText, shadowRamp);
                    materialEditor.ShaderProperty(rampColor, "Ramp Mode", 2);
                    materialEditor.ShaderProperty(shadowType, Styles.shadowTypeText, 2);
                    materialEditor.TexturePropertySingleLine(Styles.occlusionMap, occlusionMap);
                    materialEditor.ShaderProperty(_AORAMPMODE_ON, "AO Style", 2);

                    if (material.GetTexture("_OcclusionMap") && _AORAMPMODE_ON.floatValue == 1)
                    {
                        materialEditor.ShaderProperty(occlusionStrength, "Strength", 3);
                    }
                    else
                    {
                        material.SetFloat("_OcclusionStrength", 1);
                    }

                    if (_AORAMPMODE_ON.floatValue == 0)
                    {
                        materialEditor.ShaderProperty(_OcclusionColor, "AO Color", 2);
                    }

                    materialEditor.ShaderProperty(_AOUV2, "Occlusion UV");

                    XSStyles.callGradientEditor();
                }
                //ambient
                //ramp
                //mixed
                if (rampColor.floatValue == 0)
                {
                    material.SetFloat("_WORLDSHADOWCOLOR_ON", 1);
                    material.SetFloat("_MIXEDSHADOWCOLOR_ON", 0);
                }
                if (rampColor.floatValue == 1)
                {
                    material.SetFloat("_WORLDSHADOWCOLOR_ON", 0);
                    material.SetFloat("_MIXEDSHADOWCOLOR_ON", 0);
                }
                if (rampColor.floatValue == 2)
                {
                    material.SetFloat("_WORLDSHADOWCOLOR_ON", 0);
                    material.SetFloat("_MIXEDSHADOWCOLOR_ON", 1);
                }
                //-----

                //Rimlighting
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                rimlighting = EditorGUILayout.Foldout(rimlighting, "RIMLIGHT", true);
                XSStyles.helpPopup(XSStyles.rimlightURL);
                EditorGUILayout.EndHorizontal();
                GUI.skin = null;
                if (rimlighting)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.ShaderProperty(rimStyle, Styles.rimLightTypeText);

                    if (rimStyle.floatValue == 0)
                    {
                        materialEditor.ShaderProperty(rimWidth, Styles.rimWidthText, 2);
                        materialEditor.ShaderProperty(rimIntensity, Styles.rimIntText, 2);
                        materialEditor.ShaderProperty(rimColor, "Rimlight Tint", 2);
                    }

                    if (rimStyle.floatValue == 1)
                    {
                        materialEditor.ShaderProperty(rimWidth, Styles.rimWidthText, 2);
                        materialEditor.ShaderProperty(rimIntensity, Styles.rimIntText, 2);
                        materialEditor.ShaderProperty(rimColor, "Rimlight Tint", 2);
                    }

                    if (rimStyle.floatValue == 2)
                    {
                        material.SetFloat("_RimIntensity", 0);
                    }
                }
                //----

                //emission
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                emission = EditorGUILayout.Foldout(emission, "EMISSION", true);
                XSStyles.helpPopup(XSStyles.emissionsURL);
                EditorGUILayout.EndHorizontal();
                GUI.skin = null;
                if (emission)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.ShaderProperty(emissiveToggle, "Emission");
                    if (emissiveToggle.floatValue == 0)
                    {
                        materialEditor.TexturePropertySingleLine(Styles.emissText, emissiveTex, emissiveColor);
                        materialEditor.ShaderProperty(EmissTintToColor, "Tint To Diffuse");
                        materialEditor.ShaderProperty(EmissScaleWithLight, "Scale With Light");
                        if (EmissScaleWithLight.floatValue == 0)
                        {
                            materialEditor.ShaderProperty(EmissionPower, "Threshold", 2);
                        }
                        materialEditor.ShaderProperty(UseUV2Emiss, "Emission UV");
                    }
                    else
                    {
                        material.SetColor("_EmissiveColor", Color.black);
                    }
                }
                //-----

                //specular
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                specular = EditorGUILayout.Foldout(specular, "SPECULAR", true);
                XSStyles.helpPopup(XSStyles.specularURL);
                EditorGUILayout.EndHorizontal();
                EditorGUI.BeginChangeCheck();
                if (specular)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.ShaderProperty(UseSpecular, "Specular");
                    if (UseSpecular.floatValue == 0)
                    {
                        materialEditor.TexturePropertySingleLine(Styles.specMapText, specMap);
                        GUI.skin = null;
                        materialEditor.TextureScaleOffsetProperty(specMap);
                        materialEditor.TexturePropertySingleLine(Styles.specPatternText, specPattern);
                        materialEditor.TextureScaleOffsetProperty(specPattern);
                        materialEditor.ShaderProperty(stylizedType, "Specular Type");
                        materialEditor.ShaderProperty(specStyle, "Specular Style");
                        if (stylizedType.floatValue == 1)
                        {
                            material.SetFloat("_ANISTROPIC_ON", 1);
                            materialEditor.ShaderProperty(aX, "Length", 3);
                            materialEditor.ShaderProperty(aY, "Width", 3);
                        }
                        else
                        {
                            material.SetFloat("_ANISTROPIC_ON", 0);
                            materialEditor.ShaderProperty(specArea, Styles.SmoothnessText, 3);
                        }
                        materialEditor.ShaderProperty(specIntensity, Styles.sintensityText, 3);
                        materialEditor.ShaderProperty(_SpecularUv2, "Specular UV");
                        materialEditor.ShaderProperty(_SpecularPatternUv2, "Pattern UV");
                    }
                    else
                    {
                        material.SetFloat("_SpecularIntensity", 0);
                    }
                }
                //-----

                //metallic
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                reflections = EditorGUILayout.Foldout(reflections, "REFLECTIONS", true);
                XSStyles.helpPopup(XSStyles.reflURL);
                EditorGUILayout.EndHorizontal();
                GUI.skin = null;
                if (reflections)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.ShaderProperty(useRefl, "Reflections");
                    if (useRefl.floatValue == 0)
                    {
                        materialEditor.ShaderProperty(reflType, "Reflection Style");
                        material.EnableKeyword("_REFLECTIONS_ON");
                        //pbr
                        if (reflType.floatValue == 0)
                        {
                            materialEditor.TexturePropertySingleLine(Styles.bakedCube, bakedCube);
                            material.SetFloat("_PBRREFL_ON", 1);
                            material.SetFloat("_MATCAP_ON", 0);
                            material.SetFloat("_MATCAP_CUBEMAP_ON", 0);
                            materialEditor.TexturePropertySingleLine(Styles.MetalMap, metalMap);

                            if (!material.GetTexture("_MetallicMap"))
                            {
                                materialEditor.ShaderProperty(metal, "Metallic", 2);
                            }
                            else
                            {
                                material.SetFloat("_Metallic", 1);
                            }

                            materialEditor.ShaderProperty(reflSmooth, "Smoothness", 2);
                            materialEditor.ShaderProperty(_MetallicUv2, "Metal/Rough UV");
                        }
                        //matcap
                        if (reflType.floatValue == 1)
                        {
                            material.SetFloat("_MATCAP_ON", 1);
                            material.SetFloat("_MATCAP_CUBEMAP_ON", 0);
                            material.SetFloat("_PBRREFL_ON", 0);
                            materialEditor.ShaderProperty(matcapStyle, "Blend Mode");
                            materialEditor.TexturePropertySingleLine(Styles.Matcap, metalMap);
                            materialEditor.TexturePropertySingleLine(Styles.MatcapMask, roughMap);
                            materialEditor.ShaderProperty(metal, "Intensity", 2);
                            materialEditor.ShaderProperty(reflSmooth, "Blur", 2);
                        }
                        //bakedcubemap
                        if (reflType.floatValue == 2)
                        {
                            material.SetFloat("_MATCAP_CUBEMAP_ON", 1);
                            material.SetFloat("_MATCAP_ON", 0);
                            material.SetFloat("_PBRREFL_ON", 0);
                            materialEditor.TexturePropertySingleLine(Styles.bakedCube, bakedCube);
                            materialEditor.TexturePropertySingleLine(Styles.MetalMap, metalMap);

                            if (!material.GetTexture("_MetallicMap"))
                            {
                                materialEditor.ShaderProperty(metal, "Metallic", 2);
                            }
                            else
                            {
                                material.SetFloat("_Metallic", 1);
                            }

                            materialEditor.ShaderProperty(reflSmooth, "Smoothness", 2);
                            materialEditor.ShaderProperty(_MetallicUv2, "UVSet");
                        }
                    }
                    else
                    {
                        material.DisableKeyword("_REFLECTIONS_ON");
                        material.SetFloat("_PBRREFL_ON", 0);
                        material.SetFloat("_MATCAP_ON", 0);
                        material.SetFloat("_MATCAP_CUBEMAP_ON", 0);
                    }
                }
                //-----

                //Subsurface Scattering
                XSStyles.Separator();
                EditorGUILayout.BeginHorizontal();
                subsurface = EditorGUILayout.Foldout(subsurface, "SUBSURFACE SCATTERING", true);
                XSStyles.helpPopup(XSStyles.sssURL);
                GUI.skin = null;
                EditorGUILayout.EndHorizontal();
                if (subsurface)
                {
                    XSStyles.SeparatorThin();
                    materialEditor.ShaderProperty(UseSSS, "Subsurface Scattering");
                    if (UseSSS.floatValue == 0)
                    {
                        materialEditor.TexturePropertySingleLine(Styles.thicknessMap, ThicknessMap);
                        materialEditor.ShaderProperty(invertThickness, "Invert", 3);
                        materialEditor.ShaderProperty(ThicknessMapPower, "Power", 3);
                        materialEditor.ShaderProperty(SSSCol, "Subsurface Color", 2);
                        materialEditor.ShaderProperty(SSSDist, "Displacement", 2);
                        materialEditor.ShaderProperty(SSSPow, "Sharpness", 2);
                        materialEditor.ShaderProperty(SSSIntensity, "Intensity", 2);
                    }
                    else
                    {
                        material.SetFloat("_SSSIntensity", 0);
                    }
                }
                else
                {
                    if (UseSSS.floatValue == 1)
                    {
                        material.SetFloat("_SSSIntensity", 0);
                    }
                }
                //-----



                GUI.skin = null;
                if (advMode.floatValue == 1)
                {
                    XSStyles.Separator();
                    advancedSettings = EditorGUILayout.Foldout(advancedSettings, "ADVANCED SETTINGS", true);
                    if (advancedSettings)
                    {
                        XSStyles.SeparatorThin();
                        // GUILayout.Label(Styles.advancedOptions, EditorStyles.boldLabel);
                        //Stencil
                        GUILayout.Label("Stencil Buffer", EditorStyles.boldLabel);
                        materialEditor.ShaderProperty(colorMask, colorMask.displayName, 2);
                        materialEditor.ShaderProperty(stencil, stencil.displayName, 2);
                        materialEditor.ShaderProperty(stencilComp, stencilComp.displayName, 2);
                        materialEditor.ShaderProperty(stencilOp, stencilOp.displayName, 2);
                        materialEditor.ShaderProperty(stencilFail, stencilFail.displayName, 2);
                        materialEditor.ShaderProperty(stencilZFail, stencilZFail.displayName, 2);
                        materialEditor.ShaderProperty(ztest, ztest.displayName, 2);
                        materialEditor.ShaderProperty(zwrite, zwrite.displayName, 2);
                        materialEditor.ShaderProperty(RampBaseAnchor, "Ramp Anchor", 2);

                        // Reset ZWrite/ZTest
                        XSStyles.ResetAdv(material);
                        XSStyles.ResetAdvAll(material);

                        // disable pass toggle
                        // disableOutlinePass = EditorGUILayout.Toggle("Disable Outline Pass", disableOutlinePass);
                        // if(disableOutlinePass == true)
                        //     material.SetShaderPassEnabled("Always", false);
                        // else
                        //     material.SetShaderPassEnabled("Always", true);
                    }
                }
                if (advMode.floatValue == 0)
                {
                    XSStyles.CallResetAdv(material);
                }
            }
        }
        DoFooter();
    }