Home Game Development unity – Why when switching between scenes the loading transitions is loading empty scene before loading the scene in index 1?

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

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

[ad_1]

I have two scenes: Opening Scene and Game Scene.

The logic is to play the Opening Scene and then fade out then when the Game Scene has loaded to fade in when the Game Scene is already loaded.

the problem is that after doing the fadeout it’s showing empty scene it’s like doing fade in or changing the alpha color of the canvas group and showing empty scene then after a second or two loading the second scene.

but the fade out/in should be making the smooth transition between the scenes.

the canvasgroupfader script is attached to the canvas in the Opening Scene and the VideoManager script is attached to empty gameobject also in the Opening Scene.

this is the canvas group fade script:

using UnityEngine;

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

    private float targetAlpha;
    private float startAlpha;
    private float startTime;

    private bool isFading;

    private void Start()
    {
        canvasGroup = GetComponent<CanvasGroup>();
    }

    private void Update()
    {
        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 methods to initiate fading
    public void FadeIn()
    {
        StartFade(0f, 1f);
    }

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

and this is how I’m using it:

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

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

    private bool isVideoFinished = false;

    private void Start()
    {
        player.prepareCompleted += Player_prepareCompleted;
        player.loopPointReached += Player_loopPointReached;
        videoFileName.text = player.clip.name;
    }

    private void Player_prepareCompleted(VideoPlayer source)
    {
        isVideoFinished = false;
    }

    private void Player_loopPointReached(VideoPlayer source)
    {
        VideoFinished(source);
    }

    void Update()
    {
        if (player.frameCount > 0 && isVideoFinished == false)
        {
            progress.fillAmount = (float)player.frame / (float)player.frameCount;
        }

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

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

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

    private void SkipToPercent(float pct)
    {
        var frame = player.frameCount * pct;
        player.frame = (long)frame;
    }

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

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

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

    private void VideoFinished(VideoPlayer source)
    {
        isVideoFinished = true;
        progress.fillAmount = 1f;

        canvasGroupFader.FadeOut(); // Start fade-out effect
        StartCoroutine(LoadSceneAfterFade()); // Start loading the new scene after the fade-out
    }

    private IEnumerator LoadSceneAfterFade()
    {
        // Wait for the fade-out effect to complete
        yield return new WaitForSeconds(canvasGroupFader.fadeDuration);

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

        // Wait until the new scene is fully loaded
        while (!asyncLoad.isDone)
        {
            yield return null;
        }

        // Start the fade-in effect after the new scene is loaded
        canvasGroupFader.FadeIn();
    }

    private void OnDestroy()
    {
        player.prepareCompleted -= Player_prepareCompleted;
        player.loopPointReached -= Player_loopPointReached;
    }
}

[ad_2]