一個簡單的2D閃爍效果
阿新 • • 發佈:2019-01-13
國際慣例,先上圖
先網上隨便找個序列圖,切割一下做成動畫
然後給spriterenderer設定材質
Shader "Sprites/Diffuse Flash" { Properties { [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} _SelfIllum ("Self Illumination",Range(0.0,1.0)) = 0.0 _FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0 _Color ("Tint", Color) = (1,1,1,1) [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" } Cull Off Lighting Off ZWrite Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha CGPROGRAM #pragma surface surf Lambert alpha vertex:vert #pragma multi_compile DUMMY PIXELSNAP_ON sampler2D _MainTex; fixed4 _Color; float _FlashAmount,_SelfIllum; struct Input { float2 uv_MainTex; fixed4 color; }; void vert (inout appdata_full v, out Input o) { #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH) v.vertex = UnityPixelSnap (v.vertex); #endif v.normal = float3(0,0,-1); UNITY_INITIALIZE_OUTPUT(Input, o); o.color = _Color; } void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color; o.Albedo = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount); o.Emission = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount) * _SelfIllum; o.Alpha = c.a; } ENDCG } Fallback "Transparent/VertexLit" }
開始擼程式碼
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; [RequireComponent(typeof(SpriteRenderer))] public class flash : MonoBehaviour { public AnimationCurve m_Curve; public float m_duration = 1; private SpriteRenderer m_spriteRenderer; private Material m_Material; // Use this for initialization void Start () { m_spriteRenderer = GetComponent<SpriteRenderer>(); m_Material =m_spriteRenderer.material; } IEnumerator EvaluateCureve(UnityAction<float> update) { float time = 0; while (time<=m_duration) { float value = m_Curve.Evaluate(time / m_duration); time += Time.deltaTime; update.Invoke(value); yield return null; } } public void OnMouseDown() { StartCoroutine(EvaluateCureve((value)=>m_Material.SetFloat("_FlashAmount",value))); } }