Search Unity

Trouble with Addressables.DownloadDependenciesAsync

Discussion in 'Addressables' started by stevenchristian20, Mar 14, 2020.

  1. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    Hey,

    I am trying to Download an addressable from my server when I click a button to a scene. The goal is on button click to check for an update addressable and download it while the scene loads. A menu screen will activate and show a slider bar progress, while things work in the background. The addressable download is based on an AddressableLabel "comicbookName." I keep getting an "error CS0138." Here is the code I am using:


    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement;
    using UnityEngine.SceneManagement;
    using System;
    using System.Collections.Generic;
    using UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle;
    //using UnityEngine.AsyncOperation;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.AddressableAssets.ResourceLocators;

    public class DownloaderServerScript : MonoBehaviour
    {
    public GameObject loadingScreen;
    public Slider slider;
    public Text progressText;
    public AssetLabelReference comicbookName;

    public void DownloadfromServer()
    {
    StartCoroutine(LoadAsynchronously());
    }

    private IEnumerator LoadAsynchronously()
    {
    AsyncOperation asyncOperation = Addressables.DownloadDependenciesAsync(comicbookName);

    loadingScreen.SetActive(true);

    while (!asyncOperation.isDone)
    {
    float progress = Mathf.Clamp01(asyncOperation.progress / .9f);

    progressText.text = progress * 100f + "%";

    slider.value = progress;

    yield return null;
    }
    }
    }


    I keep getting an error:

    Assets\Scripts\DownloaderServerScript.cs(9,7): error CS0138: A 'using namespace' directive can only be applied to namespaces; 'AsyncOperationHandle' is a type not a namespace. Consider a 'using static' directive instead


    Please help!
     
  2. PacoLabs

    PacoLabs

    Joined:
    Jun 29, 2017
    Posts:
    31
    Hi,
    DownloadDependenciesAsync returns an AsyncOperationHandle and not an AsyncOperation.
     
  3. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    How do I fix it? Do I change AsyncOperation to AsyncOperationHandle?
     
  4. PacoLabs

    PacoLabs

    Joined:
    Jun 29, 2017
    Posts:
    31
    Yes, if you want to use DownloadDependenciesAsync.
    You'll also have to adapt your code, because the API is slightly different between AsyncOperation and AsyncOperationHandle.
     
  5. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    Ok I am not sure how to do that. Could you help me out with it?
     
  6. PacoLabs

    PacoLabs

    Joined:
    Jun 29, 2017
    Posts:
    31
    This should compile :

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.AddressableAssets;
    5. using UnityEngine.ResourceManagement.AsyncOperations;
    6.  
    7. public class DownloaderServerScript : MonoBehaviour
    8.  
    9. {
    10.     public GameObject loadingScreen;
    11.     public Slider slider;
    12.     public Text progressText;
    13.     public AssetLabelReference comicbookName;
    14.  
    15.     public void DownloadfromServer()
    16.     {
    17.         StartCoroutine(LoadAsynchronously());
    18.     }
    19.  
    20.     private IEnumerator LoadAsynchronously()
    21.     {
    22.         AsyncOperationHandle asyncOperation = Addressables.DownloadDependenciesAsync(comicbookName);
    23.         loadingScreen.SetActive(true);
    24.         while (!asyncOperation.IsDone)
    25.         {
    26.             float progress = Mathf.Clamp01(asyncOperation.PercentComplete / .9f);
    27.             progressText.text = progress * 100f + "%";
    28.             slider.value = progress;
    29.             yield return null;
    30.         }
    31.     }
    32. }
    33.  
    This is quite a standard programming thing, where you have to see the APIs provided by Unity, and implement them accordingly.
    For instance, here is the doc for AsyncOperationHandle:
    https://docs.unity3d.com/Packages/c...ent.AsyncOperations.AsyncOperationHandle.html

    May I suggest you to read some different programming tutorials, like :
    https://unity3d.com/learning-c-sharp-in-unity-for-beginners
    You should learn useful things that will help you achieve your goals.
     
    JoRangers and stevenchristian20 like this.
  7. stevenchristian20

    stevenchristian20

    Joined:
    Dec 23, 2019
    Posts:
    29
    Thanks I will check those out
     
    PacoLabs likes this.