2

I have created a game with a main menu in a scene and I have a second scene that has the actual game in it. When the user taps the play button on main menu scene it loads the actual game scene but the problem is that it takes too much time. What should i do?

Here is the code I'm using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour {

    public void PlayGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

    }
    public void QuitGame()
    {
        Debug.Log("Ho gaya....! Mera Ho gya");
        Application.Quit();
    }
}
Isma
  • 14,604
  • 5
  • 37
  • 51
Fazil Mahesania
  • 101
  • 1
  • 3
  • 18
  • 4
    As you don't really grant any insight on what the scene contains. Nor what `to much time` actually accounts to (1 second, 1 minute, 1 hour) it will be really hard to give an proper answer. – MX D Jun 29 '18 at 11:29
  • if you're scene has many meshes textures etc it will be unavoidable to take long time to load, so I advise you to create a loading bar and load it async :) – Daahrien Jun 29 '18 at 12:03
  • As other users pointed out already, you need to provide more information. You could start by adding the code inside your game scene class. – Isma Jun 29 '18 at 12:08

2 Answers2

4

You can use LoadSceneAsync to load the level in advance, but not activate it, you get a callback when the load is done, and than you can simply activate the scene when the user presses the button which should be instant

zambari
  • 4,797
  • 1
  • 12
  • 22
1

Alternatively to the above, you could also wait until said button press to call SceneManager.LoadSceneAsync, but display a curtain (aka loading screen) while the actual game scene loads. Coroutines are you friend here, as you can wait for the completion of an AsyncOperation by yield returning it from a Coroutine IE

var curtain = Instantiate(CurtainPrefab);
DontDestoryOnLoad(curtain);
yield return SceneManager.LoadSceneAsync(gameSceneIndex);

// Don't allow unload to clean up this object or it'll stop the coroutine before destroying the curtain!
DontDestroyOnLoad(gameObject);
yield return SceneManager.UnloadSceneAsync(mainMenuSceneIndex);
Destroy(curtain);

// Do this last or the Coroutine will stop short
Destroy(gameObject);
David
  • 10,458
  • 1
  • 28
  • 40