I have a working Async script, but I’d like the level to load in the background, and for the load to not actually take place unless a bool is true. For example, the player is traveling from point A to point B to point C, and I want the async to start when the player gets to point B, but for the actual scene loading to take effect until the player is at point C. So if the player never made it to point C, the level would never actually load. Is this possible?
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadScreen : MonoBehaviour
{
// Start is called before the first frame update
public Slider slider;
public Text progressText;
public void loadlevel (int sceneIndex){
StartCoroutine(LoadAsynchronously(sceneIndex));
}
IEnumerator LoadAsynchronously(int sceneIndex){
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
while (!operation.isDone){
float progress = Mathf.Clamp01(operation.progress / .9f);
slider.value = progress;
if (progress<0.99f){
progressText.text = progress *100f + "%";
}else{
progressText.text = "Please Wait";
}
//debug.Log(progress);
yield return null;
}
}