Skip to content

Instantly share code, notes, and snippets.

@ilhamhe
Last active July 22, 2019 16:40
Show Gist options
  • Save ilhamhe/3f60570103d384fba5c8f4c97f2270a4 to your computer and use it in GitHub Desktop.
Save ilhamhe/3f60570103d384fba5c8f4c97f2270a4 to your computer and use it in GitHub Desktop.
c# script to manipulate/use sprite flash
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteFlash : MonoBehaviour {
public Color flashColor;
public float flashDuration;
Material mat;
private IEnumerator flashCoroutine;
private void Awake() {
mat = GetComponent<SpriteRenderer>().material;
}
private void Start()
{
mat.SetColor("_FlashColor", flashColor);
}
private void Update() {
if(Input.GetKeyDown(KeyCode.Space))
Flash();
}
private void Flash(){
if (flashCoroutine != null)
StopCoroutine(flashCoroutine);
flashCoroutine = DoFlash();
StartCoroutine(flashCoroutine);
}
private IEnumerator DoFlash()
{
float lerpTime = 0;
while (lerpTime < flashDuration)
{
lerpTime += Time.deltaTime;
float perc = lerpTime / flashDuration;
SetFlashAmount(1f - perc);
yield return null;
}
SetFlashAmount(0);
}
private void SetFlashAmount(float flashAmount)
{
mat.SetFloat("_FlashAmount", flashAmount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment