Search Unity

Basic Addressable functionality

Discussion in 'Addressables' started by LoftySky, Sep 20, 2018.

  1. LoftySky

    LoftySky

    Joined:
    Mar 30, 2015
    Posts:
    13
    Hey,

    Struggling a bit to find examples or docs on how to do some basic things with Addressables.

    1. How can I tell if a remote file is available and valid for the current build?

    2. How can I tell how much of a file is downloaded?

    3. How can I tell if a remote file was cached and is valid?

    Cheers,
     
  2. s-vovik

    s-vovik

    Joined:
    Mar 24, 2017
    Posts:
    15
    You don't need to mess with files. Insted you need to think in terms of assets.

    1. The best way to find that out is try to load an asset
    2. It's wery simple, look at code
    3. You don't need this. Addressables will do that for you. Just check the result of IAsyncOperation after loading.

    Code (CSharp):
    1. // Loading some asset
    2. public void Load(string inAssetAddress)
    3. {      
    4.     // No need to wory about does it local or remote? does it cashed? Does it already has been loaded?
    5.     var loadOperation = Addressables.LoadAssets<GameObject>(inAssetAddress, LoadComplete);
    6.     // Start coroutine to update progress bar
    7.     StartCoroutine(LoadProgress(loadOperation));
    8. }
    9.  
    10. // When you loading asset you also loading all it dependensys
    11. // This callback will fire every time some asset will be downloaded
    12. private void LoadComplete(IAsyncOperation<GameObject> inOperation)  
    13. {
    14.    if (inOperation.Status == AsyncOperationStatus.Succeeded)
    15.       Debug.Log("Downloaded " + inOperation.Result.name)
    16.    else
    17.       Debug.Log("Error: " + inOperation.OperationException.Message)
    18. }
    19.  
    20. // This method will update progress bar and wait untill all assets will be downloaded
    21. private IEnumerator LoadProgress(IAsyncOperation<IList<GameObject>> inOperation)
    22. {
    23.     while (!inOperation.IsDone)
    24.     {
    25.         if (_loaderView != null)
    26.         {        
    27.          
    28.   _loaderView.SetProgress(inOperation.PercentComplete);
    29.              _loaderView.SetStateLabel(string.Format("LOADING {0}...", inOperation.Current));
    30.             }
    31.         if (inOperation.PercentComplete == 1)
    32.             break;
    33.         yield return new WaitForSeconds(0.1f);
    34.     }
    35. }
     
  3. k3ndro

    k3ndro

    Joined:
    Dec 8, 2016
    Posts:
    28

    Mine only shows percentage value of 0 and jumps to 1.. can't see float values. Please let me know what is wrong?
     
  4. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    please ensure you are on the latest (1.2.4+) version of addressables. download operations should show a proper % as of that release. If not let us know.
     
  5. SErAphNero

    SErAphNero

    Joined:
    Sep 11, 2018
    Posts:
    2
    Can not,
    I Don't konw why...
    help me please.

    Code (CSharp):
    1.    
    2.  
    3. public static AsyncOperationHandle<GameObject> model;
    4. public Text progressText;
    5.  
    6. public IEnumerator Start()
    7.     {
    8.         model = Addressables.LoadAssetAsync<GameObject>("model");
    9.  
    10.         while (!model.IsDone)
    11.         {
    12.             progressText.text = Math.Floor(model.PercentComplete) * 100 + "%";
    13.             yield return 1;
    14.         }
    15.         if (model.IsDone)
    16.         {
    17.  
    18.         }
    19.         yield return model;
    20.     }
     
  6. CodeBombQuinn

    CodeBombQuinn

    Joined:
    Apr 17, 2018
    Posts:
    23
    I think your Math.Floor is making every percentage 0. And then 0 * 100 = 0.

    Try: int(model.PercentComplete * 100) + "%";

    casting to int will drop any decimal places.
     
  7. SErAphNero

    SErAphNero

    Joined:
    Sep 11, 2018
    Posts:
    2
    thanks for your reply. But I change to:
    progressText.text = model.PercentComplete.ToString();
    Mine only shows percentage value of 0 and jumps to 1.. can't see float values. Please let me know what is wrong?
    1.2.4 version
     
  8. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Doesn't work for us, always jumps from 0 to 1, even on latest packages. Testing on device, loading from server
     
  9. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Are you sure this thing needs downloading? If it has been cached, then all Unity is doing is a load, which I believe Unity just reports 0 until it's loaded, then 1.

    What if you change your code to model = Addressables.DownloadDependenciesAsync("model");

    Assuming that call gives you some multiple (more than 1) frames at a % of 0, then jumps to 1, we definitely have a bug.


    If this is the first and only call you make to addressables, the first 50% is just the init op. So I'd try two things.
    1. do a manual Addressables.InitializeAsync() first, and wait until that's done to do your download. That'll verify that your download/load is indeed taking multiple frames, and you're not actually just waiting on a remote catalog update.
    2. turn off "remote catalog" in your top level settings, and go back to not manually doing init (undo test 1). With this off, init should be instantaneous. so the % should jump to 50% right away.


    Please let me know how these experiments go. We very easily could have a bug, but I'd need more context to understand what it is you're seeing.
     
    DevilCult likes this.
  10. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Hi Bill,

    In our case DownloadDependenciesAsync returns a correct progress report, but LoadAssetAsync does not (yes i'm sure it needs downloading, we clear cache just before to debug)

    There are multiple frames at 0 which then jumps to 1, and it also reports 1 for multiple frames (is that expected?)
     
  11. k3ndro

    k3ndro

    Joined:
    Dec 8, 2016
    Posts:
    28
    Hi Bill, i'm still having the same problem.
    Version 1.2.4

    Remote loading from Amazon S3, using Play Packed Mode
    I'm using a ScriptableObject with dependencies. But even when I tested with a simple GameObject or TextAsset, it still won't show the actual percentage.

    Code (CSharp):
    1.   void Start()
    2.     {
    3.    
    4.         var content = Addressables.LoadAssetAsync<ScenarioData>("content");
    5.         content.Completed += LoadManager_Completed1;
    6.  
    7.         StartCoroutine(LoadProgress(content));
    8.     }
    9.  
    10.     private IEnumerator LoadProgress(AsyncOperationHandle<ScenarioData> content)
    11.     {
    12.         while (!content.IsDone)
    13.         {
    14.             Debug.LogWarning((float)content.PercentComplete);
    15.  
    16.             if (content.PercentComplete == 1)
    17.                 break;
    18.  
    19.             yield return new WaitForSeconds(0.05f);
    20.         }
    21.  
    22.         yield return null;
    23.     }
    24.  
    25.     private void LoadManager_Completed1(AsyncOperationHandle<ScenarioData> obj)
    26.     {
    27.         Debug.LogWarning("Done loading");
    28.     }
    29.  
     

    Attached Files:

    Last edited: Oct 15, 2019
  12. k3ndro

    k3ndro

    Joined:
    Dec 8, 2016
    Posts:
    28
    Any update on this? I am using

    Any update on this? I am using 2018.4.5f1
     
  13. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Hey @k3ndro I'm helping look into this. I'm having trouble seeing the issue on our end. Loading a simple game object I'm getting at least some level of granularity on the percent complete. Looking over our change log the only thing changed that even remotely touches this is when we set our progress callback methods. That might affect it if your bundles were cached. Are your bundles cached? If they are and you use Caching.ClearCache() does it show anything different?

    Would you feel comfortable sharing your project or a small project that can reproduce the issue? You can submit the project on a ticket through Unity and post the case number here so I can check it out.
     
  14. k3ndro

    k3ndro

    Joined:
    Dec 8, 2016
    Posts:
    28
    Thanks for your reply @DavidUnity3d . I have submitted a ticket. Case #1194737. Does submitting a ticket include uploading my sample project?
     
  15. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    @k3ndro it looks like your sample project was included. Thanks!
     
  16. Gunasekhar

    Gunasekhar

    Joined:
    Jul 20, 2013
    Posts:
    3
    Any update?
     
  17. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    @Gunasekhar our QA wasn't able to reproduce it on package version 1.3.3 or higher. Are you using 1.3.3+? @k3ndro you said you're using 1.2.4? If you update to 1.3.3 or higher do you still have the problem?
     
  18. MrBum

    MrBum

    Joined:
    Oct 7, 2016
    Posts:
    6
    @DavidUnity3d Anyupdate?
    I used unity 2018.4.14f1 and addressble 1.5 still happend. I was custom create bundle provider and see percent downloaded and see PercentComplete return value not correct.
     
  19. satchell

    satchell

    Joined:
    Jul 2, 2014
    Posts:
    107
    Not sure if this is still an issue but I was looking into a preload scene and this thread helped. Wanted to share if any one was looking. Unity 2019.3.0f4 Addrss 1.5

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AddressableAssets;
    6. using UnityEngine.ResourceManagement.AsyncOperations;
    7. using TMPro;
    8.  
    9. public class PreLoader : MonoBehaviour
    10. {
    11.     public PlayerSO playerSO;
    12.     private AsyncOperationHandle<GameObject> player;
    13.     public TextMeshProUGUI loadingProgress;
    14.  
    15.     void Start()
    16.     {
    17.         loadingProgress.text = string.Format("Loading: {0}%", 0);
    18.         if (!playerSO.mpc.player)
    19.         {
    20.             player = Addressables.LoadAssetAsync<GameObject>("Base");
    21.             player.Completed += OnLoadDone;
    22.         }
    23.         else
    24.         {
    25.             Addressables.LoadSceneAsync("Part 06 Addressable");
    26.         }
    27.     }
    28.    
    29.     private void OnLoadDone(AsyncOperationHandle<GameObject> obj)
    30.     {
    31.  
    32.         if (obj.Status == AsyncOperationStatus.Failed)
    33.         {
    34.             Debug.Log("Failed to load hazards, retrying in 1 second...");
    35.             Invoke("LoadHazards", 1);
    36.             return;
    37.         }
    38.         playerSO.mpc.player = obj.Result;
    39.     }
    40.        
    41.     void Update()
    42.     {
    43.         if (player.IsValid())
    44.         {
    45.             loadingProgress.text = string.Format("Loading: {0}%", (int)(player.PercentComplete * 100));
    46.             if (playerSO.mpc.player)
    47.             {
    48.                 Addressables.ReleaseInstance(player);
    49.                 Addressables.LoadSceneAsync("Part 06 Addressable");
    50.             }
    51.         }
    52.     }
    53. }
    54.  
    55.  
     
    Last edited: Jan 2, 2020
  20. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    @satchell good to see you on here! I hit you up on Youtube about help with Addressables and AR for DLC. Quick question: what is "Part 06 Addressable" and "Base"? The problem I am having is that I can build an addressable, upload it to my Firebase Storage, and download it to my device. The download appear on my android app info as "Data" not "Cache". I'm not sure if I am downloading it right, and also how to load it into the scene.
     
    satchell likes this.
  21. satchell

    satchell

    Joined:
    Jul 2, 2014
    Posts:
    107
    @stevenchristian20 So for what I was doing in that script is stashing the object in a Scriptable Object(SO) then clearing the Addressables cache. Pardon for the lack of explanation, :)! After the SO has it I'll reference it there. But in certain aspects like a store front I might chose to call the addressables cache opposed to the SOs reference. Say if you are updating the store items regularly... I highly recommend using Unity's tutorials and example projects for the proper usage.

    This is what I would probably try to build from https://assetstore.unity.com/packages/essentials/tutorial-projects/endless-runner-sample-game-87901
     
    stevenchristian20 likes this.
  22. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    Awesome thanks. I saw a video of this but I didn't think to try it out like that! Thanks as always
     
    satchell likes this.
  23. DhiaSendi

    DhiaSendi

    Joined:
    May 16, 2018
    Posts:
    43
    Solved by just changing "obj.PercentComplete" by "obj.GetDownloadStatus().Percent"

    Code (CSharp):
    1.  IEnumerator OnLoad(AsyncOperationHandle obj)
    2.       {
    3.           while (obj.IsDone == false)
    4.           {
    5.               yield return new WaitForSeconds(.1f);
    6.               progressImg.fillAmount = obj.GetDownloadStatus().Percent;
    7.               percentCopmlete1.text = obj.GetDownloadStatus().Percent * 100f + "%".ToString();
    8.           }
    9.        
    10.       }
    11.  
    Unity 2018.4LTS
    Addressables 1.16.16
     
    Last edited: Mar 16, 2021
  24. NCura

    NCura

    Joined:
    Jun 16, 2018
    Posts:
    1
    How did you get "GetDownloadStatus()" ? It's not available for my AsyncOperationHandle.

    From the doc : "In some instances, the information will not be available. This can happen if the operation is dependent on the initialization operation for addressables."
     
    Last edited: Mar 16, 2021
  25. DhiaSendi

    DhiaSendi

    Joined:
    May 16, 2018
    Posts:
    43
    Addressables version ?