- Home /
how to download Asset Bundles from website to Application.dataPath across platforms ?
I'm putting an exported bundle online , it can be download but I only know how windows functions , i know nothing of Linux nor Mac.
I would have to redirect the download path to the folder hosting the executable game file bit i do not know how to accomplish that .
how can i get this done ?
when i have the bundle in the file location i then set the path for which the bundle is fetched using
Application.dataPath
then after writing the appropriate code , the bundles should be instantiated in my game on play.
Answer by Tomer-Barkan · Dec 08, 2013 at 06:09 AM
The AssetBundle itself is just the assets loaded into memory, it cannot be saved to the disk. On the other hand, the WWW object, downloads the file itself, so before converting the WWW result into an AssetBundle, you can save it to the disk, using WWW.bytes to get the bytes, and File.WriteAllBytes() to save to disk.
See complete answer here:
http://answers.unity3d.com/questions/19522/how-to-download-an-asset-bundle-to-local-hard-driv.html
In your case, you'll want:
File.WriteAllBytes(Application.dataPath + "/" + filename, bytes);
The game is not going to be what triggers a download . I dont want the download to take place via the game . the user will be the one who clicks a download button which will then trigger the download .
I take your response into consideration . should i just tell the uses "copy and paste your download to this folder..."
currently i access and instantiate bundles into my game if the name of the bundle to be instantiated exists in a particular folder .
I do that using Unities cache loading example
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : $$anonymous$$onoBehaviour {
public string BundleURL;
public string AssetName;
public int version;
void Start() {
StartCoroutine (DownloadAndCache());
}
IEnumerator DownloadAndCache (){
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == ""){
GameObject ts = (GameObject)Instantiate(bundle.mainAsset);
ts.name = (bundle.mainAsset.name.Replace("(Clone)",""));
}
else
Instantiate(bundle.Load(AssetName));
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
I don't get it... According to the code you posted, the download is being done by Unity. If you want the download done elsewhere, simply have the other tool, whatever it is, download the bundle and place it in the correct folder, then have unity load the bundle from the file system. What exactly are you unable to do?
there are two download stages , one where the uses goes online and downloads a file which is an asset bundle .
the bundle by default will not be downloaded into the game application folder so I need to get the bundle to the folder.
but i dont want users to see where the folder is located now how the asset is imported into the game.
once the bundle is in the game application folder . the code I posted will search that folder and instantiate the object.
I think I found a way to get the bundle downloaded from the internet to the games application folder . I will use a file browse dialogue within the game . so the user simply clicks on the downloaded bundle from there and it is copied into the game application folder .
Yes, that should work, as long as the game application has permission to access the file wherever it is located before copying it to the game application folder.
There's a usage example at the bottom of the link.
The idea is that you create an instance of the FileBrowser
, and then every frame that you want it displayed you call it's OnGUI()
method from within the $$anonymous$$onoBehaviour
's OnGUI()
.
In the constructor of the FileBrowser
you need to pass it a callback method (`FileSelectedCallback` in the example), that will be called automatically once a user selects a file.
So basically you can just use the example as is, but change the callback FileSelectedCallback
to do whatever you want with the selected file, and change the OnGUI$$anonymous$$ain
to display the layout that you want BEFORE the file browser is opened (ie everything that is outside the if
.