- Home /
Loading images from AssetBundle
I am able to extract and instantiate a prefab from the asset bundle using the code below. But I wish to extract some png sprites from the asset bundle. Also I need to follow a hierarchy because I am loading those sprites onto gameobjects at runtime. After much searching on the internet, I couldn't find a solution. Any help? Thanks.
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour {
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 == "")
Instantiate(bundle.mainAsset);
else{
GameObject go = (GameObject)Instantiate(bundle.LoadAsset(AssetName));
go.transform.SetParent(GameObject.Find("Canvas_Overlayed").transform);
go.GetComponent<assetbundleprefab>().init();
}
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
Comment
Best Answer
Answer by digzou · May 23, 2015 at 07:06 AM
Ok I managed to find it :
Sprite newSprite = bundle.LoadAsset<Sprite>("assets/resources/images/snap.png");