Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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:
    14
    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:
    17

    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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:
    8
    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:
    215
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    954
    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.
     
  10. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    215
    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:
    17
    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 at 4:16 AM