Skip to main content
2 of 4
added 223 characters in body; edited title

Instead of using yield wait for seconds can I use a UI button for async?

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;
        }
    }
}