Last active
May 21, 2016 17:14
-
-
Save Sinhyub/7e2a5011ee9d949a4ae550d37035b342 to your computer and use it in GitHub Desktop.
Unity Asset Bundle Loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AssetBundleHelper.cs | |
// | |
// Created by Sinhyub Kim on 5/22/15. | |
// | |
//#define LOAD_ASSETBUNDLE_FROM_LOCAL | |
//#define SKIP_ASSETDOWNLOAD_ON_EDITOR | |
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class AssetBundleHelper | |
{ | |
// Download or cache assetbundle | |
public delegate void LoadAssetBundleCallback(bool isSuccess, AssetBundle assetBundle); | |
public static IEnumerator LoadAssetBundle(string assetBundleURL, int versionNumber, LoadAssetBundleCallback callback) | |
{ | |
while (!Caching.ready) | |
yield return null; | |
using(WWW www = WWW.LoadFromCacheOrDownload (assetBundleURL, versionNumber)) | |
{ | |
yield return www; | |
if (www.error != null) | |
{ | |
callback(false, null); | |
throw new UnityException("WWW download:" + www.error); | |
} | |
AssetBundle bundle = www.assetBundle; | |
callback(bundle!=null, bundle); | |
bundle.Unload(false); | |
www.Dispose(); | |
} | |
} | |
// Download or cache assetbundle to load an object from the assetbundle. | |
public delegate void LoadAssetCallback<AssetObjectType>(bool isSuccess, AssetObjectType asset) where AssetObjectType : Object; | |
public static IEnumerator LoadAsset<AssetObjectType>(string assetName, string assetBundleURL, int versionNumber, LoadAssetCallback<AssetObjectType> callback) where AssetObjectType : Object | |
{ | |
// #if (UNITY_EDITOR&&SKIP_ASSETDOWNLOAD_ON_EDITOR) | |
// // Different format of AssetName is required between Resources.Load and assetbundle.LoadAsset. | |
// // So belows might not work | |
// AssetObjectType assetObject = Resources.Load<AssetObjectType>(assetName); | |
// callback(assetObject!=null, assetObject); | |
// yield return null; | |
// #else | |
while (!Caching.ready) | |
yield return null; | |
using(WWW www = WWW.LoadFromCacheOrDownload (assetBundleURL, versionNumber)) | |
{ | |
yield return www; | |
if (www.error != null) | |
{ | |
callback(false, null); | |
throw new UnityException("WWW download:" + www.error); | |
} | |
AssetBundle bundle = www.assetBundle; | |
AssetBundleRequest request = bundle.LoadAssetAsync<AssetObjectType>(assetName); | |
yield return request; | |
AssetObjectType assetObject = request.asset as AssetObjectType; | |
callback(assetObject!=null, assetObject); | |
bundle.Unload(false); | |
www.Dispose(); | |
} | |
//#endif | |
} | |
} | |
// an example to load a sprite from an asset bundle | |
public class ExampleTestObj : MonoBehaviour { | |
public Image testImage; | |
// 1. AssetBundleTest.unity3d is an asset bundle including AssetBundleTestImage (.png) | |
void LoadFromLocal() | |
{ | |
string assetBundleLocalPath = "file://"+System.IO.Path.Combine ( Application.dataPath, "Resources/AssetBundleTest/AssetBundleTest.unity3d" ); | |
StartCoroutine( AssetBundleHelper.LoadAsset<Sprite>("AssetBundleTestImage", assetBundleLocalPath, 1, (bool isSuccess, Sprite spriteObject) => | |
{ | |
if (isSuccess == false) | |
{ | |
throw new UnityException ("Failed to load test sprite"); | |
return; | |
} | |
this.testImage.sprite = spriteObject; | |
})); | |
} | |
void LoadFromWeb() | |
{ | |
string assetBundleURL = "http://www.googledrive.com/host/0B6Dbmbd_-1doSGZKemlBT2taTk0/AssetBundleTest.unity3d"; | |
StartCoroutine( AssetBundleHelper.LoadAsset<Sprite>("AssetBundleTestImage", assetBundleURL, 1, (bool isSuccess, Sprite spriteObject) => | |
{ | |
if (isSuccess == false) | |
{ | |
throw new UnityException ("Failed to load test sprite"); | |
return; | |
} | |
this.testImage.sprite = spriteObject; | |
})); | |
} | |
void Awake() | |
{ | |
#if !LOAD_ASSETBUNDLE_FROM_LOCAL | |
LoadFromWeb(); | |
#else | |
LoadFromLocal(); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment