Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to load an AssetBundle from the cache?

Discussion in 'Addressables' started by zhuxianzhi, Sep 30, 2019.

  1. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    24
    Code (CSharp):
    1.  internal void Start(ProvideHandle provideHandle)
    2.         {
    3.             m_AssetBundle = AssetBundle.LoadFromFile(provideHandle.Location.InternalId);
    4.             if(m_AssetBundle == null)
    5.                 Debug.LogError("the bundle failed " + provideHandle.Location.InternalId);
    6.             provideHandle.Complete(this, m_AssetBundle != null, null);
    7.         }
    provideHandle.Location.InternalId == http:/cdn/XXXX.Bundle
    Assuming that the resource bundle has been downloaded to the local, how should I convert a network address to a local address?
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    215
    Theres two options, either you've manually downloaded the bundle to the filesystem, in which case only you know the path to the bundle as it will be wherever you've chosen to download it to (look into this https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerFile.html)

    Second options is to use Unity's caching mechanism. Basically you ask unitywebrequest to load a bundle with a given hash, it then searches for it in local cache and loads it without requiring network if its available, or else downloads it from the web and caches it for future use.

    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequestAssetBundle.GetAssetBundle.html

    edit:

    sorry, just realised this is addressables section. The above is still true if you download bundles yourself
     
    zhuxianzhi likes this.
  3. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    24
    I use the following code
    Code (CSharp):
    1.  var requestOperation = UnityWebRequestAssetBundle.GetAssetBundle("http://xxxx/ab.bundle");
    2.                 requestOperation.SendWebRequest();
    3.                 m_AssetBundle = DownloadHandlerAssetBundle.GetContent(requestOperation);
    Even if the bundle is already downloaded in the cache, you can't get .GetContent synchronously. Is there any solution?

    Thanks in advance