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

Question [ICODE]LoadAssetAsync()[/ICODE] seems to freeze game's main thread for 1-2 seconds

Discussion in 'Addressables' started by ayo_420, Dec 25, 2022.

  1. ayo_420

    ayo_420

    Joined:
    Jan 23, 2022
    Posts:
    1
    I am trying out some of the Addressables' loading examples to instantiate gameobjects (mostly UI elements and particle effects) at run-time.

    I am noticing that once my game is running after pressing play, the first call I make to
    LoadAssetAsync<T>(string someKey)
    , or any other async load/instantiate method, freezes the game briefly for about 1-2 seconds. All calls thereafter with the same string key are instant. I am not subscribing to the Completion event or awaiting the result in any way - just the method call by itself causes this behaviour.

    Is this normal? I'd like to also know if I'm misunderstanding the purpose of Addressable assets - I'm trying to use them a convenient, organized, and non thread-blocking way to instantiate prefabs on demand.

    A simple example that causes this behaviour for me is:

    Code (CSharp):
    1. public class MyScript : MonoBehaviour
    2.     {
    3.         void Update()
    4.         {
    5.             if (Keyboard.current.wKey.wasPressedThisFrame)
    6.             {
    7.                 Addressables.LoadAssetAsync<Sprite>("someKey");
    8.             }
    9.         }
    10.     }
     
  2. Ksurething

    Ksurething

    Joined:
    May 2, 2020
    Posts:
    3
    Hi,
    I think it should not cause the game to hang as well.
    As a work-around, maybe you can try move the asynchronous loading of assets out of the update thread? Maybe you can start a Coroutine that loads the asset instead?

    Code (CSharp):
    1. StartCoroutine(LoadMySprite());
    2. }
    3. ...
    4. private IEnumerator LoadMySprite ()
    5. {
    6.    // async loading
    7. }
     
  3. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    Everything here should be asynchronous. The only possible idea I have it when deserialising the json data of the catalog. However 1-2 seconds is very long.
    Do you have a profiling capture that shows where the time is spent?