the code below is the code I use to load my game scene ater the intro animation plays. The way it works is that it preloads the game scene because it is big enough it takes time to load and activates it after 15 seconds.
I want to use this code in a similar manner for my main menu, but instead of preloading and waiting for seconds I want it to preload and wait for UI button.
public class IntroToForest : MonoBehaviour
{
public float WaitToLoad = 14.0f;
void Start()
{
StartCoroutine(LoadScene(WaitToLoad));
}
IEnumerator LoadScene(float WaitToLoad)
{
yield return null;
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Forest Map");
asyncOperation.allowSceneActivation = false;
while (!asyncOperation.isDone)
{
if (asyncOperation.progress >= 0.9f)
{
yield return new WaitForSeconds(WaitToLoad);
asyncOperation.allowSceneActivation = true;
}
yield return null;
}
}
}
Side note: I incorporated DmGregory's answer into my menu controller script and got this:
public class MenuController : MonoBehaviour
{
public string SceneName;
AsyncOperation preload;
void Start()
{
preload = SceneManager.LoadSceneAsync(SceneName);
preload.allowSceneActivation = false;
}
public void Go_to_game()
{
if (preload.progress < 0.9f)
{
if (PlayerPrefs.HasKey("HasSeenIntro"))
{
preload.allowSceneActivation = true;
}
else
{
PlayerPrefs.SetInt("HasSeenIntro", 1);
SceneManager.LoadScene("Intro");
}
}
}
}
The problem is that the UI button will not work.
allowSceneActivation = true
should not be gated by your progress check, otherwise you skip transitioning to the scene if it's not already loaded when the button is clicked. \$\endgroup\$