- Home /
What keyword string should I add to enable/disable when changing shader properties ?
The shader code:
Shader "Standard"
{
Properties
{
[LM_Albedo] [LM_Transparency] _Color("Color", Color) = (1,1,1,1)
[LM_MasterTilingOffset] [LM_Albedo] _MainTex("Albedo", 2D) = "white" {}
[LM_TransparencyCutOff] _AlphaTestRef("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
[LM_Glossiness] _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.0
[LM_Metallic] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
[LM_Metallic] [LM_Glossiness] _MetallicGlossMap("Metallic", 2D) = "white" {}
_BumpScale("Scale", Float) = 1.0
[LM_NormalMap] _BumpMap("Normal Map", 2D) = "bump" {}
_Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
_ParallaxMap ("Height Map", 2D) = "black" {}
_OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
_OcclusionMap("Occlusion", 2D) = "white" {}
[LM_Emission] _EmissionColor("Color", Color) = (0,0,0)
[LM_Emission] _EmissionMap("Emission", 2D) = "white" {}
_DetailMask("Detail Mask", 2D) = "white" {}
_DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
_DetailNormalMapScale("Scale", Float) = 1.0
_DetailNormalMap("Normal Map", 2D) = "bump" {}
[KeywordEnum(UV1, UV2)] _UVSec ("UV Set for secondary textures", Float) = 0
// UI-only data
[KeywordEnum(None, Realtime, Baked)] _Lightmapping ("GI", Int) = 1
[HideInInspector] _EmissionScaleUI("Scale", Float) = 1.0
[HideInInspector] _EmissionColorUI("Color", Color) = (0,0,0)
[HideInInspector] _EmissionColorWithMapUI("Color", Color) = (1,1,1)
// Blending state
[HideInInspector] _Mode ("__mode", Float) = 0.0
[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _ZWrite ("__zw", Float) = 1.0
}
And I want to change the Rendering Mode. My script:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.IMGUI;
public class ChangeShaderRenderingModes : EditorWindow
{
public enum BlendMode
{
Opaque,
Cutout,
Fade,
Transparent
}
static Material material;
static Material objectMat;
static int modeIndex = 0;
[MenuItem("Tools/Change Rendering Modes")]
public static void ShowWindow()
{
GetWindow<ChangeShaderRenderingModes>("ChangeRenderingModes");
}
private void OnGUI()
{
if(GUI.Button(new Rect(50,50,50,50), "Switch Mode"))
{
var objects = Selection.objects;
Shader shader = Shader.Find("Standard");
material = new Material(shader);
if (modeIndex == 4)
modeIndex = 0;
material.SetFloat("_Mode", modeIndex);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
GameObject go = GameObject.Find("Test");
objectMat = go.GetComponent<Renderer>().sharedMaterial;
objectMat = material;
go.GetComponent<Renderer>().sharedMaterial = material;
modeIndex++;
}
}
}
So in the editor when I click the button I see in the inspector that it's switching between the rendering modes. But I need to click the shader in the inspector so it will take affect.
I want it to take affect automatic when changing modes. Where in my script and how do I change the keyword/s and what should be the keyword/s string/s ?
Comment
Best Answer
Answer by UnityCoach · Nov 11, 2018 at 01:57 PM
Have a look at this thread, it answers your question.