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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Does AssetBundle.LoadAsset block main thread?

Discussion in 'Scripting' started by fzhfzh, May 18, 2016.

  1. fzhfzh

    fzhfzh

    Joined:
    Apr 7, 2016
    Posts:
    2
    So, we are having "GameObject references runtime script in scene file. Fixing!" bug when using AssetBundle.LoadAssetAsync. Using AssetBundle.LoadAsset fixes it.

    We are running LoadAsset in a separate corotinue, however, the documentation isn't very clean on whether LoadAsset blocks the main UI thread if it's ran on a separate coroutine. Anyone have any information about this? Thanks.
     
  2. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    You need to use LoadAsset Async to load it in the background and be able to wait for it in a co-routine.
     
  3. fzhfzh

    fzhfzh

    Joined:
    Apr 7, 2016
    Posts:
    2
    So are there any difference between using LoadAssetAsync in a coroutine and wait for it vs using LoadAsset in a Coroutine?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yes, LoadAssetAsync is Asynchronous.

    LoadAsset blocks.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Using LoadAsset in a coroutine won't get you anything over just calling it normally. Putting something in a coroutine doesn't automatically make it asynchronous and coroutines still execute on the main thread. They simply provide you the opportunity to spread the execution of your code out over multiple frames.
     
  6. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    AssetBundle.LoadFromFileAsync called via Coroutine but still using the main thread. This leads to freezing UI issue when loading a large asset bundle file. I wonder if we can call new thread (non-main thread) to use AssetBundle.LoadFromFileAsync?
     
  7. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You cannot use Unity API methods across multiple threads- they aren't thread-safe.
     
  8. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    Is there anyway to solve the issue of block UI when loading the asset bundle from file?
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Using Async in a coroutine is the closest you're going to get.
     
  10. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    Hm, Async is still in main thread with blocking UI. It seems like the only solution is to make multiple small asset bundle files :)