How to save and load a copy of the AssetBundleManifest file

Hi guys,

Using Unity’s Asset Bundle System, I am trying to achieve the following behaviour:

  1. When app opens, if wifi connection is present, download the manifest file from cloud.
  2. If no wifi available, load an older version of the manifest from cache.

However, from Unity’s asset bundle demo, the manifest file is always downloaded from the cloud via this code:

WWW download = new WWW (url);

whilst non-manifest files are downloaded using:

WWW download = WWW.LoadFromCacheOrDownload (url, m_AssetBundleManifest.GetAssetBundleHash (assetBundleName), 0));

I tried a couple of things such as using LoadFromCacheOrDownload() when new WWW() has failed (wifi not present) but I’m not sure what hash to use for the manifest file.

I can also see this comment: “For manifest assetbundle, always download it as we don’t have hash for it.” but still my heart wishes to achieve the behaviour mentioned.

Any help or insight is greatly appreciated. :slight_smile:

If you’re using Unity’s Asset Bundle Manager, open the AssetBundleManager.cs script. Within that script, there’s an Update() function that checks for finished downloads. The manifest file does not have an asset bundle name, unlike other assets, so check for a finished download that doesn’t have a name, and it will be the main manifest file.

First, find the Update() function in AssetBundleManager.cs. There’s a comment “If downloading succeeds”. Find it. That’s where you’ll start.

NOTE: There is another newer,version of the AssetBundleManager on Bitbucket. If that’s the version you’re using you can grab the finished download in the AssetBundleLoadOperation.cs script, just before m_www.Dispose(); and m_www = null;

Here’s an excerpt from for the Update function, along with where you can capture the manifest it downloads.

    private static AssetBundleManifest my_Manifest;
    void Update(){
    
    // ...code omitted from this example
    
    // If downloading succeeds.
    
    if (download.isDone) {
    						
         AssetBundle bundle = download.assetBundle;
    
         if (bundle == null) {m_DownloadingErrors.Add (keyValue.Key, string.Format ("{0} is not a valid asset bundle.", keyValue.Key));
    
         keysToRemove.Add (keyValue.Key);
    	continue;
    }
    
        //Here's where we grab the manifest
        
        if (download.assetBundle.name == "") {
        
             StartCoroutine (WaitToSaveManifest (www));
        }
    }
    
    //here's where the manifest is saved to the disk
        		public IEnumerator WaitToSaveManifest(WWW www){
    
        			string savePath = Application.persistentDataPath + Path.DirectorySeparatorChar + "iOS.bytes";
    
        			File.WriteAllBytes (savePath, www.bytes);
        
        //			ensure download bundles are not backed via iCloud, or your app will be rejected by Apple
        			UnityEngine.iOS.Device.SetNoBackupFlag(savePath);
        
        }
    
    //here's how to retrieve the manifest file from the bytes file that was saved to the disk
    IEnumerator OfflineLoading(){
    
    		byte[] readAssBytes = File.ReadAllBytes (Application.persistentDataPath + Path.DirectorySeparatorChar + "iOS.bytes");
    		
    		AssetBundle acr = AssetBundle.LoadFromMemory (readAssBytes);
    		yield return acr;
    
    		AssetBundleManifest readAssetBundle = (AssetBundleManifest)acr.LoadAsset ("AssetBundleManifest");
    		yield return readAssetBundle;
    		my_Manifest = readAssetBundle;
    //Don't initialize the asset bundle manager because it will try to download the manifest from your server and throw an error. 
//Instead, take your local manifest that you loaded from the disk, and give it to the Asset Bundle Manager, like so:
    		AssetBundleManager.AssetBundleManifestObject = readAssetBundle;

    //Done!
    	}

I imagine there are more elegant solutions to this problem. If you’d prefer to avoid altering the code in Unity’s Asset Bundle Manager, you could download the manifest yourself using WWW.LoadFromCacheOrDownload and passing in the address to the manifest file, instead of intercepting the one that the ABM is downloading.