01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Shader "Custom/ScrollUVs" { Properties { _MainTex ( "Albedo (RGB)" , 2D) = "white" {} _ScrollXSpeed ( "X Scroll Speed" , Range(0, 10)) = 2 _ScrollYSpeed ( "Y Scroll Speed" , Range(0, 10)) = 2 } SubShader { Tags { "RenderType" = "Opaque" } LOD 200 CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows sampler2D _MainTex; fixed _ScrollXSpeed; fixed _ScrollYSpeed; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutputStandard o) { fixed2 scrolledUV = IN.uv_MainTex; fixed xScrollValue = _ScrollXSpeed * _Time.y; fixed yScrollValue = _ScrollYSpeed * _Time.y; scrolledUV += fixed2(xScrollValue,yScrollValue); // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, scrolledUV); o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" } |
float4 _Time : Time (t/20, t, t*2, t*3), use to animate things inside the shaders
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | Shader "Custom/AnimateSprites" { Properties { _MainTex ( "Base (RGB)" , 2D) = "white" {} _CellAmount ( "Cell Amount" , float ) = 0.0 _Speed ( "Speed" , Range(0.01, 32)) = 12 } SubShader { Tags { "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert sampler2D _MainTex; float _CellAmount; float _Speed; struct Input { float2 uv_MainTex; }; void surf (Input IN, inout SurfaceOutput o) { //Lets store our UVs in a seperate variable float2 spriteUV = IN.uv_MainTex; //Lets calculate the width of a singe cell in our //sprite sheet and get a uv percentage that each cel takes up. float cellUVPercentage = 1.0 / _CellAmount; //Lets get a stair step value out of time so we can increment //the uv offset float timeVal = fmod(_Time.y * _Speed, _CellAmount); timeVal = ceil(timeVal); //Animate the uv's forward by the width precentage of //each cell float xValue = spriteUV.x; xValue += timeVal; xValue *= cellUVPercentage; spriteUV = float2(xValue, spriteUV.y); half4 c = tex2D (_MainTex, spriteUV); o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | using UnityEngine; using System.Collections; public class SpriteAnimator : MonoBehaviour { public float speed = 0.0f; public int cellAmount = 0; float timeValue = 0.0f; void start() { transform.renderer.material.SetFloat( "_cellAmount" ,cellAmount); } // Update is called once per frame void FixedUpdate () { timeValue = Mathf.Ceil(Time.time * speed % cellAmount); transform.renderer.material.SetFloat( "_TimeValue" , timeValue); } } |
联系客服