一个简单的2D闪烁效果

国际惯例,先上图

一个简单的2D闪烁效果_第1张图片

先网上随便找个序列图,切割一下做成动画

然后给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();
        m_Material =m_spriteRenderer.material;
	}

    IEnumerator EvaluateCureve(UnityAction 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)));
    }
}

 

你可能感兴趣的:(unity)