IEnumerator Fader(float time, bool fadeSwitch, FadeEnded fadeEnded) { Color a; while (time >= 0) { a = image.color; if (fadeSwitch) a.a -= ((1 / time) / 100); else a.a += ((1 / time) / 100); image.color = a; time -= 0.01f; yield return new WaitForSeconds(0.01f); } a = image.color; if (fadeSwitch) a.a = 0; else a.a = 1; image.color = a; fadeEnded(); yield return null; }
public void StartFade(float time, FadeEnded fadeEnded) { float alpha = image.color.a; print(alpha); if (alpha > 0) StartCoroutine(Fader(time, true, fadeEnded)); else StartCoroutine(Fader(time, false, fadeEnded)); }
IEnumerator FadeCoroutine(float duration, float targetOpacity, Color color) { Debug.Log("Fading!"); // Announce this is happening. FadeEventArgs fadeArgs = new FadeEventArgs(); fadeArgs.targetOpacity = targetOpacity; // When this fader gets invisible, the screen is being faded in. When it gets // fully-opaque, it's being faded out. if (targetOpacity == 0) { fadeArgs.fadeTarget = FadeStyle.fadeIn; } else if (targetOpacity == 1) { fadeArgs.fadeTarget = FadeStyle.fadeOut; } FadeStarted.Invoke(fadeArgs); // Decide how the fading will be done, and apply the color. float timer = 0; float baseOpacity = alpha; // Do the fading over time. while (timer < duration) { timer += Time.deltaTime; alpha = Mathf.Lerp(baseOpacity, targetOpacity, timer / duration); yield return(null); } alpha = targetOpacity; // In case the duration is 0. // Announce this is done. FadeEnded.Invoke(fadeArgs); coroutine = null; }