Question by
thezoeyteh · Jan 24 at 04:21 PM ·
scenescene-loadingasyncasynchronous
How do I preload multiple scenes?
I want to make it so that I can preload a specific list of scenes for each scene and unload them once the player is out of that scene (eg: scene 1 preloads 2,3,4 then the player enters scene 2 it loads 1,5,6 and unloads 3 and 4).
I would just like to preload a number of scenes without loading them but this code loads them as soon as they finish loading. So far my code for my first scene looks like this (preloads scene 2,9,8, unloads 3,5,6,4,10):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneMan : MonoBehaviour
{
public void Start()
{
StartCoroutine(PreloadReady());
}
IEnumerator PreloadReady() {
AsyncOperation TwoLoad = SceneManager.LoadSceneAsync("Room2");
AsyncOperation NineLoad = SceneManager.LoadSceneAsync("Room9");
AsyncOperation EightLoad = SceneManager.LoadSceneAsync("Room8");
AsyncOperation ThreeUnload = SceneManager.UnloadSceneAsync("Room3");
AsyncOperation FiveUnload = SceneManager.UnloadSceneAsync("Room5");
AsyncOperation SixUnload = SceneManager.UnloadSceneAsync("Room6");
AsyncOperation FourUnload = SceneManager.UnloadSceneAsync("Room4");
AsyncOperation TenUnload = SceneManager.UnloadSceneAsync("Room10");
TwoLoad.allowSceneActivation = false;
NineLoad.allowSceneActivation = false;
EightLoad.allowSceneActivation = false;
while(!TwoLoad.isDone)
{
yield return null;
Debug.Log ("Loaded");
}
while(!NineLoad.isDone)
{
yield return null;
Debug.Log ("Loaded");
}
while(!EightLoad.isDone)
{
yield return null;
Debug.Log ("Loaded");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Correct use of Application.UnloadLevel ? 1 Answer
Load Scene in Background and Load When a Button is Clicked 0 Answers
How do you call a function once 2 Answers
How to load scene with async + delay 1 Answer
How to properly call Async Task 0 Answers