So I guess this is finaly the last big error before I can run my game properly.
So I have build my game to make a little play test. In my version you go from starting a new game into a cutscene. Once the cutscene has finished I am using the normal fader, that we also used for the portal, to fade in into the first level.The fade is doing its job and is doing the fade out with the setted times.But then it takes like 10-20 seconds until the next scene loads. So after the fade its like a black screen for 10-20 seconds until the game finaly loads and I am asking myself why? Is it cause of the LoadSceneAsync?
This is the code of my cutscene, which loads the Coroutine once the video has finished to play:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.SceneManagement;
using RPG.Saving;
using System;
namespace RPG.SceneManagement
{
public class VideoPlayback : MonoBehaviour
{
public VideoPlayer VideoPlayer; // Drag & Drop the GameObject holding the VideoPlayer component
public string SceneName ;
[SerializeField] float fadeInTime= 0.2f;
[SerializeField] float fadeOutTime = 0.3f;
[SerializeField] int sceneToLoad = 2;
void Start()
{
VideoPlayer.loopPointReached += LoadScene;
}
void LoadScene(VideoPlayer vp)
{
StartCoroutine(LoadFirstScene());
}
IEnumerator LoadFirstScene()
{
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeOutTime);
SceneManager.LoadSceneAsync(sceneToLoad);
fader.FadeIn(fadeInTime);
}
}
}
And here we have my Fader and Portal Scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Script for creating a fade when changing between scenes
namespace RPG.SceneManagement
{
public class Fader : MonoBehaviour
{
CanvasGroup canvasGroup;
Coroutine currentActiveFade = null;
private void Awake()
{
canvasGroup = GetComponent<CanvasGroup>();
}
public void FadeOutImmediate()
{
canvasGroup.alpha = 1;
}
public Coroutine FadeOut(float time)
{
return Fade(1, time);
}
public Coroutine FadeIn(float time)
{
return Fade(0,time);
}
public Coroutine Fade(float target,float time)
{
if (currentActiveFade != null)
{
StopCoroutine(currentActiveFade);
}
currentActiveFade = StartCoroutine(FadeRoutine(target,time));
return currentActiveFade;
}
private IEnumerator FadeRoutine(float target, float time)
{
while (!Mathf.Approximately(canvasGroup.alpha, target))
{
canvasGroup.alpha = Mathf.MoveTowards(canvasGroup.alpha, target,Time.unscaledDeltaTime/time);
yield return null;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.AI;
using RPG.Control;
using UnityEngine.Events;
//Script for creating a portal and change scenes seemesly
namespace RPG.SceneManagement
{
public class Portal : MonoBehaviour
{
enum DestinationIdentifier
{
A, B, C, D, E
}
[SerializeField] int sceneToLoad = 1;
[SerializeField] Transform spawnPoint;
[SerializeField] DestinationIdentifier destination;
[SerializeField] float fadeInTime = 1f;
[SerializeField] float fadeOutTime = 1f;
[SerializeField] float fadeWaitTime = 0.5f;
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
StartCoroutine(Transition());
}
}
private IEnumerator Transition()
{
if(sceneToLoad < 0)
{
Debug.LogError("Scene to Load not set");
yield break;
}
//SceneManager.LoadScene(sceneToLoad);
DontDestroyOnLoad(gameObject);
Fader fader = FindObjectOfType<Fader>();
SavingWrapper wrapper = FindObjectOfType<SavingWrapper>();
PlayerController playerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
playerController.enabled = false;
yield return fader.FadeOut(fadeOutTime);
//Save current Level
wrapper.Save();
yield return SceneManager.LoadSceneAsync(sceneToLoad);
PlayerController newplayerController = GameObject.FindWithTag("Player").GetComponent<PlayerController>();
newplayerController.enabled = false;
//Load current Level
wrapper.Load();
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
wrapper.Save();
yield return new WaitForSeconds(fadeWaitTime);
fader.FadeIn(fadeInTime);
newplayerController.enabled = true;
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
GameObject player =GameObject.FindWithTag("Player");
player.GetComponent<NavMeshAgent>().enabled = false;
player.transform.position = otherPortal.spawnPoint.position;
player.transform.rotation = otherPortal.spawnPoint.rotation;
player.GetComponent<NavMeshAgent>().enabled = true;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsOfType<Portal>())
{
if (portal == this) continue;
if(portal.destination != destination) continue;
return portal;
}
return null;
}
}
}