Ejemplo n.º 1
0
    IEnumerator Fade(float fade_time)
    {
        FadeOutStart?.Invoke();
        FadeOut();
        yield return(new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length));

        FadeOutDone?.Invoke();
        yield return(new WaitForSeconds(fade_time));

        FadeInStart?.Invoke();
        FadeIn();
        yield return(new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length));

        FadeInDone?.Invoke();
    }
Ejemplo n.º 2
0
    private IEnumerator Loading(string sceneName)
    {
        isLoading = true;
        bool        isLoadingMenu    = sceneName == "Menu";
        bool        isLoadingGame    = sceneName == "Game";
        bool        isLoadingCredits = sceneName == "Credits";
        float       timer            = 0;
        const float fadeDuration     = 1;

        while (timer < fadeDuration)
        {
            faderGroup.alpha = Mathf.Lerp(0, 1f, timer / fadeDuration);
            timer           += Time.deltaTime;
            yield return(null);
        }
        faderGroup.alpha = 1;
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);

        while (!asyncLoad.isDone)
        {
            yield return(null);
        }

        if (isLoadingGame)
        {
            audioSources[1].Play();
        }
        if (isLoadingCredits)
        {
            audioSources[2].Play();
        }

        timer = 0;
        const float duration = 2;

        while (timer < duration)
        {
            float t = timer / duration;
            if (isLoadingGame)
            {
                audioSources[0].volume = Mathf.Lerp(menuVolume, 0, t);
                audioSources[1].volume = Mathf.Lerp(0, gameVolume, t);
            }
            else if (isLoadingCredits)
            {
                audioSources[2].volume = Mathf.Lerp(creditsVolume, 0, t);
                audioSources[0].volume = Mathf.Lerp(0, menuVolume, t);
            }
            else if (isLoadingMenu)
            {
                if (audioSources[1].isPlaying)
                {
                    audioSources[1].volume = Mathf.Lerp(audioSources[1].volume, 0, t);
                }
                if (audioSources[2].isPlaying)
                {
                    audioSources[2].volume = Mathf.Lerp(audioSources[2].volume, 0, t);
                }
                audioSources[0].volume = Mathf.Lerp(0, menuVolume, t);
            }
            faderGroup.alpha = Mathf.Lerp(1, 0f, t);
            timer           += Time.deltaTime;
            yield return(null);
        }
        FadeInDone?.Invoke();
        audioSources[0].volume = isLoadingGame ? 0 : menuVolume;
        audioSources[1].volume = isLoadingGame ? gameVolume : 0;
        audioSources[2].volume = isLoadingCredits ? creditsVolume : 0;
        audioSources[0].Stop();
        faderGroup.alpha = 0;
        isLoading        = false;
        if (!isLoadingGame && !isLoadingCredits)
        {
            MenuLoaded?.Invoke();
        }
    }