Skip to content

Level Select

Jayden Tarrance edited this page Apr 26, 2024 · 9 revisions

Menu Buttons

Uses the player's raycast object to click a button. When a button is clicked, the respective LoadAsync() function will be run.

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
        }
    }
  • Office
    Loads the office scene on click.
  • Traffic Collision
    Loads the traffic collision scene on click.

Clone this wiki locally