Skip to content

Instantly share code, notes, and snippets.

@rustybailey
Last active November 23, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustybailey/6290005aae6e18b4379397b81949e74d to your computer and use it in GitHub Desktop.
Save rustybailey/6290005aae6e18b4379397b81949e74d to your computer and use it in GitHub Desktop.
Flash/Blink 2D Sprite with Shader that uses the Sprite Renderer's color

Inspired by LordNed's Diffuse Flash Shader, but fixed it so that it uses the SpriteRenderer's Color

Use this shader if you want to flash/blink a sprite. You would likely want to use this when the player or an enemy is damaged.

  1. Copy Diffuse_Flash.shader into your project
  2. Create a new Material using this Shader
  3. Find the game object with a Sprite Renderer that you want to flash and change its Material to the new one you created
  4. Copy SpriteFlasher.cs into your project and add it as a new component on your game object
  5. Within your scripts, whenever you need to flash the sprite, you can simply call GetComponent<SpriteFlasher>().Flash()
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
[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;
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 = v.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 UnityEngine;
public class SpriteFlasher : MonoBehaviour
{
[SerializeField] int numberOfFlashes = 2;
[SerializeField] float durationBetweenFlashes = 0.05f;
// In order for the flashing to work, the objectToFlash must have
// a SpriteRenderer and be using the Flash Material
[SerializeField] GameObject objectToFlash;
private SpriteRenderer spriteRenderer;
private IEnumerator flashingCoroutine;
// Start is called before the first frame update
void Start()
{
spriteRenderer = objectToFlash.GetComponent<SpriteRenderer>();
}
public void Flash()
{
if (spriteRenderer)
{
if (flashingCoroutine != null)
{
StopCoroutine(flashingCoroutine);
}
flashingCoroutine = InternalFlash();
StartCoroutine(flashingCoroutine);
}
}
private IEnumerator InternalFlash()
{
bool makeSpriteWhite = true;
// Iterate twice the length of times - that way numberOfFlashes is
// "how many times is it turned on", not "how many times does it flip"
for (int i = 0; i < numberOfFlashes * 2; i++)
{
spriteRenderer.material.SetFloat("_FlashAmount", makeSpriteWhite ? 1f : 0f);
makeSpriteWhite = !makeSpriteWhite;
yield return new WaitForSeconds(durationBetweenFlashes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment