Search Unity

Cached asset bundle folder structure

Discussion in 'Asset Bundles' started by unity_SRZXyHN7P3fFcA, Jun 28, 2021.

  1. unity_SRZXyHN7P3fFcA

    unity_SRZXyHN7P3fFcA

    Joined:
    Oct 27, 2019
    Posts:
    10
    Hi,

    I'm new to asset bundles, and got to where I actually download bundles and have them in the cache. Now, I'd like to load them later on into memory using AssetBundle.LoadFromFile(bundlePath).

    But to get the right bundlePath, I had to Path.Combine(Caching.defaultCache.path, bundleName, bundleHash, "__data") because this is the structure bundles get stored in inside the cache folder. FYI, there is another file next to each "__data" file called "__info".

    My question is: is this right? Is this a reliable way to construct the path and load the bundles from files that will work across different platforms? I'm on Unity 2020.3, building for StandaloneOSX, but will build for Android and iOS soon.
     
    Last edited: Jun 30, 2021
  2. unity_SRZXyHN7P3fFcA

    unity_SRZXyHN7P3fFcA

    Joined:
    Oct 27, 2019
    Posts:
    10
  3. TobyKaos

    TobyKaos

    Joined:
    Mar 4, 2015
    Posts:
    214
    Hello,
    to load from streamingAssets folder I used:
    Code (CSharp):
    1.  
    2. AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(System.IO.Path.Combine(Application.streamingAssetsPath, abName));
    3.  
    where abName is for instance: "prefabs"


    To load from the web and use cache
    Code (CSharp):
    1. Hash128 hash = assetBundleManifest.GetAssetBundleHash(abName);
    2.      
    3. CachedAssetBundle cached = new CachedAssetBundle(abName.Replace(".", "_"), hash);
    4.                 using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(url, cached, 0))
    5.                 { ... }

    I embed the manifest created for Android, or for Ios to have the hash of assetBundles. This way I can clean old assetBundles when I upload a new version on my server.

    Then:
    Load asset bundle manifest
    Load assetBundles
    Clean assetBundles (look at existing function)



    if you want to loadfromfile nor in streamingAssets folder you can use the persistantApplicationPath instead.
     
    Last edited: Sep 30, 2021