Shader "Unlit/Custom/Shader05"
{
Properties
{
_MainTex("MainTex",2D) = "white"{}
[Toggle] _INVERT ("Invert color?", Float) = 0 // _INVERT 必须是大写的 _Invert 不可以
[KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0
[Enum(ON, 0, OFF, 1)] _Enum("Test Enum", float) = 1
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
#pragma shader_feature _INVERT_ON
struct AppData
{
float4 position:POSITION;
float2 uv:TEXCOORD0;
};
struct V2f
{
float4 position:POSITION;
float2 uv:TEXCOORD0;
};
V2f vert(AppData appdata)
{
V2f o;
o.position = UnityObjectToClipPos(appdata.position);
o.uv = appdata.uv;
return o;
}
fixed4 frag(V2f i):SV_Target
{
fixed4 col = tex2D(_MainTex,i.uv);
#ifdef _INVERT_ON
return fixed4(1,1,0,1);
#else
return fixed4(1,0,0,1);
#endif
}
ENDCG
}
}
}
在材质面板就有一个toggle 的选框
也可以使用C# 代码控制 这个是关键字,而不是变量
if (Input.GetKeyUp(KeyCode.A)) { //GetComponent<Renderer>().material.SetFloat("_INVERT_ON",1); 这个无效 GetComponent<Renderer>().material.EnableKeyword("_INVERT_ON"); } if (Input.GetKeyUp(KeyCode.D)) { //GetComponent<Renderer>().material.SetFloat("_INVERT_ON",0); GetComponent<Renderer>().material.DisableKeyword("_INVERT_ON"); }
参考网页
https://zhuanlan.zhihu.com/p/552155864
https://blog.csdn.net/qq_33789001/article/details/124102179
https://zhuanlan.zhihu.com/p/93194054
https://blog.csdn.net/qq_43718731/article/details/127104592?spm=1001.2014.3001.5502