- Home /
How to load an AssetBundle from sdcard
I am trying to load an asset bundle from my external storage (sdcard). On regular android it is easy
Environment.getExternalDirector().getPath() + file.extendsion
I cant seem to find where the external directory is with unity. I have tired
file://sdcard/file.ext
file:///sdcard/file.ext
file:///mnt/sdcard/file.ext and everything in between.
I am trying to load my asset bundles with this code AssetBundle bundle = AssetBundle.CreateFromFile("file:///sdcard/animals.unity3d"); but it keeps saying
Failed to open file at path: file:///sdcard/animals.unity3d or whichever string i use for the file. I am getting really frustrated with this,and I cannot find any answers here that actually work.
Answer by Raigex · Jul 10, 2012 at 09:30 PM
I have found the answer. I was trying to open with Create from file but if you use www.LoadfromCachOrDownload("file://sdcard/filename.ext") it will load it up without any delay. Although I am not sure if it will work on all android devices so if anyone has a better way please post it.
hey raigex, this dint work for me. Im testing using HTC sensation can you tel me , how you got it work lil elaborately :)
You should not use file://sdcard/filename.ext If you want to do this then here is how. Save the file you want to load in Application.persistantDataPath/your/sub/folders
then load it like this:
[code] WWW.LoadFromCacheOrDownload("file://" + Application.persistantDataPath + "/your/sub/folders/file.extension", 0) [/code]
This will load from a local copy of the file.
Answer by sanketprabhu · Apr 22, 2016 at 10:25 AM
AssetBundle is platform specific . While exporting AssetBundle make sure that its exported for Android platform.
Then that Example.unity3d file on SDCard and just attach following C# code snip. to empty GameObject.
example URL of file on Android device SD-card
**string bundleURL = "file:///mnt/sdcard/AndroidCube.unity3d";**
C# Script:
void Start() {
StartCoroutine(DownloadAndCache());
}
// Update is called once per frame
IEnumerator DownloadAndCache() {
while(!Caching.ready)
yield return null;
// example URL of file on PC filesystem (Windows)
// string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d";
// example URL of file on Android device SD-card
string bundleURL = "file:///mnt/sdcard/AndroidCube.unity3d";
using (WWW www = WWW .LoadFromCacheOrDownload(bundleURL, Version)) {
yield return www;
if (www .error != null)
throw new UnityException("WWW Download had an error: " + www .error);
// Load and retrieve the AssetBundle
AssetBundle bundle = www .assetBundle;
Ten do whatever you want do with that assets. Imp: make sure in Manifest permission for ReadExternalSDcard is there
Enjoy....