Skip to main content
edited body
Source Link

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 incorperatedincorporated 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.

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 incorperated 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.

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.

Updated question with no issue regarding an answer.
Source Link

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 incorperated 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.

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

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 incorperated 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.

added 223 characters in body; edited title
Source Link

How to preload Instead of using yield wait for seconds can I use a scene in the background with async and load on uiUI button for async?

I am working on a main menu, but I am having a problem where the game scene takes too long to load socode below is the code I want to use async to load my game scene ater the intro animation plays. The way it works is that it preloads the game scene on startbecause it is big enough it takes time to load and activates it after 15 seconds.

From there when the user selects the ui button to take them to the game scene iI want to set syncOperation.allowSceneActivationuse this code in a similar manner for my main menu, but instead of preloading and waiting for seconds I want it to truepreload and wait for UI button. Here is the code

public class LoadScenesIntroToForest : 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;
        }
    }
}

How to preload a scene in the background with async and load on ui button?

I am working on a main menu, but I am having a problem where the game scene takes too long to load so I want to use async to load the scene on start.

From there when the user selects the ui button to take them to the game scene i want to set syncOperation.allowSceneActivation to true. Here is the code

public class LoadScenes : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(LoadScene());
    }
    IEnumerator LoadScene()
    {
        yield return null;
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Forest Map");
        asyncOperation.allowSceneActivation = false;
        while (!asyncOperation.isDone)
        {
            if (asyncOperation.progress >= 0.9f)
            {
                asyncOperation.allowSceneActivation = true;
            }
        yield return null;
        }
    }
}

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;
        }
    }
}
Source Link
Loading