-
Notifications
You must be signed in to change notification settings - Fork 0
Level Select
Jayden Tarrance edited this page Apr 26, 2024
·
9 revisions
Uses the player's raycast object to click a button. When a button is clicked, the respective Load<"Scene">Async() function will be run.
Every Load<"Scene">Async() function will set a string variable located in the UserManager called "nextSceneToLoad", then loads the "Loading scene".
- When a button is clicked to load the "Office", the function LoadOfficeAsync() is executed
- The variable, "nextSceneToLoad", in the UserManager is set to "Office"
- The Scene is switched to the "Loading scene"
- The variable "nextSceneToLoad" is passed to the LoadSceneAsync() function and the Office scene begins to load asynchronously
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
private void Start()
{
GameObject userMan = GameObject.Find("UserManager");
user = userMan.GetComponent<UserManager>();
if (SceneManager.GetActiveScene().name == "Loading scene")
{
StartCoroutine(LoadSceneAsync(user.nextSceneToLoad));
}
}
public void LoadOfficeAsync()
{
user.nextSceneToLoad = "Office";
SceneManager.LoadScene("Loading scene");
}
public void LoadTutorialAsync()
{
user.nextSceneToLoad = "Tutorial";
SceneManager.LoadScene("Loading scene");
}
public void LoadMainMenuAsync()
{
user.nextSceneToLoad = "Main Menu";
SceneManager.LoadScene("Loading scene");
}
IEnumerator LoadSceneAsync(string sceneName)
{
float startTime = Time.time;
Debug.Log("Loading Scene Async");
// Start loading the scene
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
asyncLoad.allowSceneActivation = false;
// While the scene loads, update the slider
while (!asyncLoad.isDone)
{
float elapsedTime = Time.time - startTime;
float remainingTime = loadingDelay - elapsedTime;
//Update slider
float progress = Mathf.Clamp01(remainingTime / loadingDelay);
loadingSlider.value = 1 - progress;
if (remainingTime <= 0)
{
asyncLoad.allowSceneActivation = true; // Allow the next scene to activate
}
yield return null; // Wait a frame before continuing the loop
}
}