Search Unity

Execute code after completing some operations

Discussion in 'Addressables' started by Remer, Apr 13, 2020.

  1. Remer

    Remer

    Joined:
    Mar 24, 2013
    Posts:
    78
    Hello,
    i have this code, where I load 4 scriptable objects, and then each of these loads 1 sprite 1 material.
    How can I understand when both the 4 SO and the other operation are completed?
    Is there any way to concatenate these operations and execute a function after they have completed?

    Code (CSharp):
    1. m_AssetSharedData.CommonRarity.LoadAssetAsync().Completed += (op) =>
    2. {
    3.     op.Result.RaritySprite.LoadAssetAsync().Completed += (op2) => m_CommonRaritySprite = op2.Result;
    4.     op.Result.RarityMaterial.LoadAssetAsync().Completed += (op2) => m_CommonRarityMaterial = op2.Result;
    5. };
    6.  
    7. m_AssetSharedData.RareRarity.LoadAssetAsync().Completed += (op) =>
    8. {
    9.     op.Result.RaritySprite.LoadAssetAsync().Completed += (op2) => m_CommonRaritySprite = op2.Result;
    10.     op.Result.RarityMaterial.LoadAssetAsync().Completed += (op2) => m_CommonRarityMaterial = op2.Result;
    11. };
    12.            
    13. m_AssetSharedData.EpicRarity.LoadAssetAsync().Completed += (op) =>
    14. {
    15.     op.Result.RaritySprite.LoadAssetAsync().Completed += (op2) => m_CommonRaritySprite = op2.Result;
    16.     op.Result.RarityMaterial.LoadAssetAsync().Completed += (op2) => m_CommonRarityMaterial = op2.Result;
    17. };
    18.            
    19. m_AssetSharedData.LegendaryRarity.LoadAssetAsync().Completed += (op) =>
    20. {
    21.     op.Result.RaritySprite.LoadAssetAsync().Completed += (op2) => m_CommonRaritySprite = op2.Result;
    22.     op.Result.RarityMaterial.LoadAssetAsync().Completed += (op2) => m_CommonRarityMaterial = op2.Result;
    23. };
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Using tasks, you can do it like this:


    Code (CSharp):
    1. async Task LoadSpritesAndMaterials()
    2. {
    3.     await Task.WhenAll(
    4.         LoadSpriteAndMaterialFromRarity(m_AssetSharedData.CommonRarity),
    5.         LoadSpriteAndMaterialFromRarity(m_AssetSharedData.RareRarity),
    6.         LoadSpriteAndMaterialFromRarity(m_AssetSharedData.EpicRarity),
    7.         LoadSpriteAndMaterialFromRarity(m_AssetSharedData.LegendaryRarity)
    8.     );
    9.  
    10.     async Task LoadSpriteAndMaterialFromRarity(Rarity rarity)
    11.     {
    12.         var data = await rarity.LoadAssetAsync().Task;
    13.  
    14.         await Task.WhenAll(LoadSprite(data.RaritySprite), LoadMaterial(data.RarityMaterial));
    15.  
    16.         async Task LoadSprite(AssetReference spriteReference)
    17.         {
    18.             m_CommonRaritySprite = await spriteReference.LoadAssetAsync().Task;
    19.         }
    20.  
    21.         async Task LoadMaterial(AssetReference materialReference)
    22.         {
    23.             m_CommonRarityMaterial = await materialReference.LoadAssetAsync().Task;
    24.         }
    25.     }
    26. }
    Then you can wait for that to complete and call your next code like this:


    Code (CSharp):
    1. await LoadSpritesAndMaterials();
    2.  
    3. // Your code here.
     
    CameronND likes this.
  3. Remer

    Remer

    Joined:
    Mar 24, 2013
    Posts:
    78
    Thanks!
    I used a slightly different method based on your code and it works perfectly.