Last active
July 22, 2019 16:40
-
-
Save ilhamhe/3f60570103d384fba5c8f4c97f2270a4 to your computer and use it in GitHub Desktop.
c# script to manipulate/use sprite flash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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