38 lines
1012 B
C#

using System.Collections;
using UnityEngine;
public class HitFlashEffect : MonoBehaviour
{
private MaterialPropertyBlock propertyBlock;
private Renderer[] renderers;
void Awake()
{
propertyBlock = new MaterialPropertyBlock();
renderers = GetComponentsInChildren<Renderer>();
}
public void Flash(float duration)
{
StartCoroutine(FlashCoroutine(duration));
}
IEnumerator FlashCoroutine(float duration)
{
// Set flash
propertyBlock.SetColor("_EmissionColor", Color.white * 2f);
foreach (var r in renderers)
{
r.GetPropertyBlock(propertyBlock);
propertyBlock.SetColor("_EmissionColor", Color.white * 2f);
r.SetPropertyBlock(propertyBlock);
}
yield return new WaitForSeconds(duration);
// Clear flash
propertyBlock.SetColor("_EmissionColor", Color.black);
foreach (var r in renderers)
r.SetPropertyBlock(propertyBlock);
}
}