unity – Why when switching between scenes the loading transitions is loading empty scene earlier than loading the scene in index 1?

unity – Why when switching between scenes the loading transitions is loading empty scene earlier than loading the scene in index 1?

[ad_1]

I’ve two scenes: Opening Scene and Sport Scene.

The logic is to play the Opening Scene after which fade out then when the Sport Scene has loaded to fade in when the Sport Scene is already loaded.

the issue is that after doing the fadeout it is exhibiting empty scene it is like doing fade in or altering the alpha coloration of the canvas group and exhibiting empty scene then after a second or two loading the second scene.

however the fade out/in ought to be making the graceful transition between the scenes.

the canvasgroupfader script is connected to the canvas within the Opening Scene and the VideoManager script is connected to empty gameobject additionally within the Opening Scene.

that is the canvas group fade script:

utilizing UnityEngine;

public class CanvasGroupFader : MonoBehaviour
{
    public float fadeDuration = 1.0f; // Period of the fade in seconds
    personal CanvasGroup canvasGroup;

    personal float targetAlpha;
    personal float startAlpha;
    personal float startTime;

    personal bool isFading;

    personal void Begin()
    {
        canvasGroup = GetComponent<CanvasGroup>();
    }

    personal void Replace()
    {
        if (isFading)
        {
            float elapsedTime = Time.time - startTime;
            float t = Mathf.Clamp01(elapsedTime / fadeDuration);

            float newAlpha = Mathf.Lerp(startAlpha, targetAlpha, t);
            canvasGroup.alpha = newAlpha;

            if (t >= 1.0f)
            {
                isFading = false;
            }
        }
    }

    public void StartFade(float startAlphaValue, float targetAlphaValue)
    {
        if (!isFading)
        {
            startAlpha = startAlphaValue;
            targetAlpha = targetAlphaValue;
            startTime = Time.time;

            isFading = true;
        }
    }

    // Public strategies to provoke fading
    public void FadeIn()
    {
        StartFade(0f, 1f);
    }

    public void FadeOut()
    {
        StartFade(1f, 0f);
    }
}

and that is how I am utilizing it:

utilizing UnityEngine;
utilizing UnityEngine.EventSystems;
utilizing UnityEngine.Video;
utilizing UnityEngine.UI;
utilizing TMPro;
utilizing System.Collections;
utilizing UnityEngine.SceneManagement;

public class VideoManager : MonoBehaviour, IDragHandler, IPointerDownHandler
{
    public VideoPlayer participant;
    public Picture progress;
    public TextMeshProUGUI videoFileName;
    public float scrollSpeed = 50f;
    public CanvasGroupFader canvasGroupFader;

    personal bool isVideoFinished = false;

    personal void Begin()
    {
        participant.prepareCompleted += Player_prepareCompleted;
        participant.loopPointReached += Player_loopPointReached;
        videoFileName.textual content = participant.clip.title;
    }

    personal void Player_prepareCompleted(VideoPlayer supply)
    {
        isVideoFinished = false;
    }

    personal void Player_loopPointReached(VideoPlayer supply)
    {
        VideoFinished(supply);
    }

    void Replace()
    {
        if (participant.frameCount > 0 && isVideoFinished == false)
        {
            progress.fillAmount = (float)participant.body / (float)participant.frameCount;
        }

        if (!isVideoFinished && participant.isPlaying && (ulong)participant.body >= participant.frameCount - 1)
        {
            VideoFinished(participant);
        }
    }

    public void OnDrag(PointerEventData eventData)
    {
        TrySkip(eventData);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        TrySkip(eventData);
    }

    personal void SkipToPercent(float pct)
    {
        var body = participant.frameCount * pct;
        participant.body = (lengthy)body;
    }

    personal void TrySkip(PointerEventData eventData)
    {
        Vector2 localPoint;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
            progress.rectTransform, eventData.place, null, out localPoint))
        {
            float pct = Mathf.InverseLerp(progress.rectTransform.rect.xMin, progress.rectTransform.rect.xMax, localPoint.x);
            SkipToPercent(pct);
        }
    }

    public void VideoPlayerPause()
    {
        if (participant != null)
            participant.Pause();
    }

    public void VideoPlayerPlay()
    {
        if (participant != null)
            participant.Play();
    }

    personal void VideoFinished(VideoPlayer supply)
    {
        isVideoFinished = true;
        progress.fillAmount = 1f;

        canvasGroupFader.FadeOut(); // Begin fade-out impact
        StartCoroutine(LoadSceneAfterFade()); // Begin loading the brand new scene after the fade-out
    }

    personal IEnumerator LoadSceneAfterFade()
    {
        // Anticipate the fade-out impact to finish
        yield return new WaitForSeconds(canvasGroupFader.fadeDuration);

        // Load the brand new scene asynchronously
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(1, LoadSceneMode.Single);

        // Wait till the brand new scene is absolutely loaded
        whereas (!asyncLoad.isDone)
        {
            yield return null;
        }

        // Begin the fade-in impact after the brand new scene is loaded
        canvasGroupFader.FadeIn();
    }

    personal void OnDestroy()
    {
        participant.prepareCompleted -= Player_prepareCompleted;
        participant.loopPointReached -= Player_loopPointReached;
    }
}

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply