Search Unity

Question Game freeze when InstantiatingAsync

Discussion in 'Addressables' started by jeppe79, Oct 7, 2020.

  1. jeppe79

    jeppe79

    Joined:
    Sep 17, 2019
    Posts:
    75
    Hello,

    The frame freezes when I run this code.
    I tried using async/await, but that throws an error saying it must be run on the Main thread.
    As I understand it, changes to a UI element must be done on the Main thread and starting the method with await will start the operation on a different thread.

    What are the options to run this without blocking the Main thread?


    Code (CSharp):
    1. private void InstantiateStuff(){
    2.  
    3.      // contentList has 1000 items
    4.  
    5.      foreach (var item in contentList)
    6.      {
    7.              prefab.InstantiateAsync(Vector3.zero, Quaternion.identity).Completed +=
    8.                 (AsyncOperationHandle<GameObject> op) =>
    9.                 {
    10.                     GameObject uiElement= op.Result;
    11.                     // Plenty more logic here populating the new uiElement from item
    12.                 };
    13.      }