Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Call AssetBundle.LoadAssetAsync in Coroutine cause engine freeze

Discussion in 'Scripting' started by desmondliu, Dec 2, 2016.

  1. desmondliu

    desmondliu

    Joined:
    Feb 4, 2015
    Posts:
    2
    When I trying to load asset from a bundle with AssetBundle.LoadAssetAsync, it workes fine. But if I start 2 Coroutines, it will cause engine freeze.
     

    Attached Files:

  2. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    The code seems fine. Could it be that bundleA has references to assets in bundleB and vice versa? attempting to open both at the same time could then deadlock.
     
  3. desmondliu

    desmondliu

    Joined:
    Feb 4, 2015
    Posts:
    2
    Thanks, JC_SummitTech! :D
    I found the problem maybe caused by SceneManager.LoadSceneAsync and AssetBundle.LoadAssetAsync.Here is my steps:
    1. Build scene into bundle.
    2. Use AssetBundle.LoadAssetAsync load this bundle.
    3. Load scene with SceneManager.LoadSceneAsync.
    4. When SceneManager.LoadSceneAsync done, load other assets with AssetBundle.LoadAssetAsync.
    Unity freezed in AssetBundle.LoadAssetAsync(Not every time).

    All assets were build into bundles, and I trying to make the whole asset loading progress is asynchronous.
     
  4. WaaghMan

    WaaghMan

    Joined:
    Jan 27, 2014
    Posts:
    241
    Necro'ing this post just to say this issue still remains in 2019.4.32f1 . Won't check on newer builds.

    This code is a guaranteed softlock.

    IEnumerator LoadStage()
    {
    var op = SceneManager.LoadSceneAsync("MainMenu", LoadSceneMode.Single);

    op.allowSceneActivation = false;

    while(op.progress < 0.9f)
    yield return null;

    var request = bundle.LoadAssetAsync<GameObject>("MenuBG");

    while(!request.isDone)
    yield return null;

    op.allowSceneActivation = true;

    }

    The workaround basically is to avoid this by loading from the bundle first, then the SceneManager.
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,840