IEnumerator FadeCoroutine(float targetOpacity, float duration = 1f) { isFading = true; blocksRaycasts = true; Debug.Log("Fading!"); FadeBegin.Invoke(); float baseOpacity = opacity; float timer = 0f; while (timer < duration) { timer += Time.deltaTime; opacity = Mathf.Lerp(baseOpacity, targetOpacity, timer / duration); //Debug.Log("Current opacity: " + opacity); yield return(null); } // Make sure to be precise opacity = targetOpacity; fadeCoroutine = null; // Makes it so the screen doesn't keep you from interacting with UI elements // when it's done. blocksRaycasts = false; isFading = false; FadeEnd.Invoke(); }
void ResetState() { Debug.Log("ScreenFader state reset!"); if (fadeCoroutine != null) { Debug.Log("Stopping fade cooroutine!"); StopCoroutine(fadeCoroutine); } opacity = 0; FadeBegin.RemoveAllListeners(); FadeEnd.RemoveAllListeners(); }