Search Unity

Question Addressables LoadAsync freeze the game

Discussion in 'Scripting' started by ShavSkelet, Aug 30, 2021.

  1. ShavSkelet

    ShavSkelet

    Joined:
    Nov 29, 2017
    Posts:
    29
    I'm trying to load some buildings in my game but for some reason, when im load and instantiate the buildings Async, the game freezes (Trying this on android mobile). But Its not supose to freeze when its asynchronous, so im i doing something wrong?
    I think the code im using is really simple, and i don't see anything wrong...

    Note :The addressable prefab is a big prefab.

    Code (CSharp):
    1. public void CargarObjetoIndividual(int i)
    2.     {
    3.         var op = Addressables.LoadAssetAsync<GameObject>(referencias[i]);   //--> load an AssetReference
    4.         op.Completed += (operation) => { referencias[i].InstantiateAsync(Vector3.zero, Quaternion.identity); }; //--> Instantiate the assetReference when its loaded
    5.        
    6.  
    7.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Did you use Logcat to see if you're getting any errors?
     
  3. ShavSkelet

    ShavSkelet

    Joined:
    Nov 29, 2017
    Posts:
    29
    No errors. i just think there are too many objects and im having better results just splitting the prefabs in small pieces. But its still weird that an async operation freezes the game even if the gameobject is too big.
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Instantiate is not async, only the loading is.

    How complex is your prefab, how many scripts it has? When you instantiate a prefab, all Awake and OnEnable methods will execute immediately on the main thread, so depending on how many of those and how much stuff they do there may be a noticeable stall.

    Also, is the stall shorter if you instantiate the same prefab again later? If yes, this might be caused by the shader being compiled by the GPU for the first time. This is an annoying limitation in mobile devices, and the only way to get around this is to try to "warm up" the shader at an earlier point, like a loading screen.