public void Update(GameTime gameTime) { if (fading) { if (fadeIn) { fadeValue += fadeSpeed; if (fadeValue >= 255) { if (loop) { fadeIn = false; } else { fading = false; } fadeValue = 255; OnFadedIn?.Invoke(this, null); } } else { fadeValue -= fadeSpeed; if (fadeValue <= 0) { if (loop) { fadeIn = true; } else { fading = false; } fadeValue = 0; OnFadedOut?.Invoke(this, null); } } byte b = (byte)fadeValue; tr.color.R = b; tr.color.G = b; tr.color.B = b; tr.color.A = b; } }
/// <summary>Use StartCoroutine() on this one.</summary> public IEnumerator FadeOut(float fadeSeconds, bool stopWhenDone = true) { float startVolume = source.volume; while (source.volume > 0) { source.volume -= startVolume * (Time.unscaledDeltaTime / fadeSeconds); yield return(null); } if (stopWhenDone) { Stop(); source.volume = initialVolume; } OnFadedOut?.Invoke(); }
private IEnumerator FadeCoroutine(CanvasGroup group, float end, float time) { float start = group.alpha; float alpha = group.alpha; float timer = 0f; while (alpha != end) { timer += Time.unscaledDeltaTime; alpha = Mathf.Lerp(start, end, timer / time); group.alpha = alpha; yield return(null); } if (end == 1f) { OnFadedIn.Invoke(); } else if (end == 0f) { OnFadedOut.Invoke(); } }