Hey,
I need some help on how to properly load resources at scene transitioning without noticing any lag.
I searched the web for this, but it seems there are only answers of asyncronous loading just the desired scene.
.
My game is constructed by having a menu scene (=current scene) and a game scene (=target scene).
.
From the menu I can select a particular level. Each level is a prefab, which should get loaded into the game scene.
.
The prefab-loading should be part of the scene transitioning procedure as loading afterwards makes no sense. The application should load the prefab after the target scene is loaded (async.progress >= 0.9f) and after both are loaded the scene should activated.
This is what I have, but it will loop forever because allowSceneActivation = true is never reached.
private IEnumerator LoadScene(string level, string prefab = ""){
float loadcount = prefab == "" ? 1f : 2f;
AsyncOperation async = SceneManager.LoadSceneAsync(level);
async.allowSceneActivation = false;
ResourceRequest prefabRequest = Resources.LoadAsync(prefab );
DontDestroyOnLoad (Instantiate (prefabRequest.asset as GameObject));
while (async.progress < 0.9f) {
loadProgress = (async.progress) / loadcount * 0.9f;
progressBar.value = loadProgress;
yield return null;
}
if(prefabRequest != null){
while (!prefabRequest.isDone) {
loadProgress = (async.progress + prefabRequest.progress) / loadcount * 0.9f;
progressBar.value = loadProgress;
yield return null;
}
}
async.allowSceneActivation = true;
}