参考链接:
https://blog.csdn.net/e295166319/article/details/60141677
https://docs.unity3d.com/ScriptReference/MaterialPropertyDrawer.html
1.Toggle
a.[Toggle] _Invert ("Invert?", Float) = 0
勾选时,表示使shader关键字_INVERT_ON生效,关键字格式为:name_ON(全部大写)
取消勾选时,表示使对应的关键字失效
要注意的是,这个关键字是要去定义的,否则Toggle无效
1 Shader "Custom/TestMaterialProperty" 2 { 3 Properties 4 { 5 [Toggle] _Invert ("Invert?", Float) = 0 6 } 7 SubShader 8 { 9 Pass 10 { 11 CGPROGRAM 12 #pragma vertex vert 13 #pragma fragment frag 14 #pragma multi_compile __ _INVERT_ON 15 16 #include "UnityCG.cginc" 17 18 struct appdata 19 { 20 float4 vertex : POSITION; 21 }; 22 23 struct v2f 24 { 25 float4 vertex : SV_POSITION; 26 }; 27 28 v2f vert (appdata v) 29 { 30 v2f o; 31 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); 32 return o; 33 } 34 35 fixed4 frag (v2f i) : SV_Target 36 { 37 fixed4 col = fixed4(1, 0, 0, 0); 38 #ifdef _INVERT_ON 39 col = fixed4(0, 1, 0, 0); 40 #endif 41 return col; 42 } 43 ENDCG 44 } 45 } 46 }
效果如下,来回勾选就可以看到颜色在切换了
b.[Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0
勾选时,表示使shader关键字ENABLE_FANCY生效
取消勾选时,表示使对应的关键字失效
1 Shader "Custom/TestMaterialProperty" 2 { 3 Properties 4 { 5 [Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0 6 } 7 SubShader 8 { 9 Pass 10 { 11 CGPROGRAM 12 #pragma vertex vert 13 #pragma fragment frag 14 #pragma multi_compile __ ENABLE_FANCY 15 16 #include "UnityCG.cginc" 17 18 struct appdata 19 { 20 float4 vertex : POSITION; 21 }; 22 23 struct v2f 24 { 25 float4 vertex : SV_POSITION; 26 }; 27 28 v2f vert (appdata v) 29 { 30 v2f o; 31 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); 32 return o; 33 } 34 35 fixed4 frag (v2f i) : SV_Target 36 { 37 fixed4 col = fixed4(1, 0, 0, 0); 38 #ifdef ENABLE_FANCY 39 col = fixed4(0, 1, 0, 0); 40 #endif 41 return col; 42 } 43 ENDCG 44 } 45 } 46 }
2.KeywordEnum
[KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
当选择None时,表示使shader关键字_OVERLAY_NONE生效,关键字格式为:属性名_枚举名(全部大写)
1 Shader "Custom/TestMaterialProperty" 2 { 3 Properties 4 { 5 [KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0 6 } 7 SubShader 8 { 9 Pass 10 { 11 CGPROGRAM 12 #pragma vertex vert 13 #pragma fragment frag 14 #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY 15 16 #include "UnityCG.cginc" 17 18 struct appdata 19 { 20 float4 vertex : POSITION; 21 }; 22 23 struct v2f 24 { 25 float4 vertex : SV_POSITION; 26 }; 27 28 v2f vert (appdata v) 29 { 30 v2f o; 31 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); 32 return o; 33 } 34 35 fixed4 frag (v2f i) : SV_Target 36 { 37 fixed4 col = fixed4(1, 1, 1, 1); 38 #if _OVERLAY_NONE 39 col = fixed4(1, 0, 0, 1); 40 #elif _OVERLAY_ADD 41 col = fixed4(0, 1, 0, 1); 42 #elif _OVERLAY_MULTIPLY 43 col = fixed4(0, 0, 1, 1); 44 #endif 45 return col; 46 } 47 ENDCG 48 } 49 } 50 }
效果如下,选择不同的选项即显示不同的颜色