public class Flash : MonoBehaviour
{
[SerializeField]
private float duration = 1f;
[SerializeField]
private float count = 1f;
[SerializeField]
private Renderer renderer;
private Color storedColor;
private float precalculatedFactor;
private float currentTime;
private IEnumerator coroutine ()
{
while (true)
{
float t = Mathf.Abs (Mathf.Sin (precalculatedFactor * currentTime));
float a = Mathf.Lerp (0f, 1f, t);
renderer.material.color = new Color (storedColor.r, storedColor.g, storedColor.b, a);
currentTime += Time.deltaTime;
yield return 1;
}
}
public void Enable (bool enabled)
{
currentTime = 0f;
if (enabled)
{
StartCoroutine (coroutine ());
}
else
{
StopAllCoroutines ();
renderer.material.color = storedColor;
}
}
void Start ()
{
precalculatedFactor = Mathf.PI * count / duration;
storedColor = renderer.material.color;
if (renderer == null)
renderer = GetComponent<Renderer> ();
}
void Update ()
{
}
}