纹理的Wrap Mode一定要设置成Repeat
Shader代码
Shader "Custom/Chapter11-ScrollingBackground" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _ScrollX ("Scroll Speed", float) = 1.0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 Pass { Tags { "LightMode"="ForwardBase" } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; float _ScrollX; struct a2v { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; half2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw; //frac(x):返回x的小数部分 o.uv += frac(float2(_ScrollX, 0.0) * _Time.y); return o; } fixed4 frag(v2f i) : Color { fixed4 c = tex2D(_MainTex, i.uv); return c; } ENDCG } } FallBack "VertexLit" }