参考链接:
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无效
Shader "Custom/TestMaterialProperty"
{
Properties
{
[Toggle] _Invert ("Invert?", Float) =
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ _INVERT_ON #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
}; struct v2f
{
float4 vertex : SV_POSITION;
}; v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(, , , );
#ifdef _INVERT_ON
col = fixed4(, , , );
#endif
return col;
}
ENDCG
}
}
}
效果如下,来回勾选就可以看到颜色在切换了
b.[Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0
勾选时,表示使shader关键字ENABLE_FANCY生效
取消勾选时,表示使对应的关键字失效
Shader "Custom/TestMaterialProperty"
{
Properties
{
[Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) =
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ ENABLE_FANCY #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
}; struct v2f
{
float4 vertex : SV_POSITION;
}; v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(, , , );
#ifdef ENABLE_FANCY
col = fixed4(, , , );
#endif
return col;
}
ENDCG
}
}
}
2.KeywordEnum
[KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
当选择None时,表示使shader关键字_OVERLAY_NONE生效,关键字格式为:属性名_枚举名(全部大写)
Shader "Custom/TestMaterialProperty"
{
Properties
{
[KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) =
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
}; struct v2f
{
float4 vertex : SV_POSITION;
}; v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = fixed4(, , , );
#if _OVERLAY_NONE
col = fixed4(, , , );
#elif _OVERLAY_ADD
col = fixed4(, , , );
#elif _OVERLAY_MULTIPLY
col = fixed4(, , , );
#endif
return col;
}
ENDCG
}
}
}
效果如下,选择不同的选项即显示不同的颜色