Created
June 5, 2016 09:31
-
-
Save Sinhyub/85afbfbd63fe69a7a84a3c1658276b5a to your computer and use it in GitHub Desktop.
download and load asset from AssetBundle with Synchronous Method (Blocking call)
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
// | |
// AssetBUndleMgr.cs | |
// | |
// Created by Sinhyub Kim 2016.06.05 | |
// | |
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class ALAssetBundleMgr | |
{ | |
// AssetBundle URL | |
public const string ASSETBUNDLE_URL_CHARGRAPHIC = "http://download.url/for_assetbundle.unity3d"; | |
// CallBack Lambda | |
public delegate void LoadAssetBundleCallback(bool isSuccess, AssetBundle assetBundle); | |
public delegate void LoadAssetCallback<AssetObjectType>(bool isSuccess, AssetObjectType asset) where AssetObjectType : Object; | |
// Asynchronous Method | |
// Download or cache assetbundle | |
public static IEnumerator AsyncLoadAssetBundle(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(); | |
} | |
} | |
// Blocking Method (Synchronous) | |
// Download or cache assetbundle | |
public static AssetBundle LoadAssetBundle(string assetBundleURL, int versionNumber) | |
{ | |
AssetBundle assetBundleObject = null; | |
IEnumerator loadAssetBundleEnumerator = AsyncLoadAssetBundle(assetBundleURL, versionNumber, (bool isSuccess, AssetBundle assetBundle) => | |
{ | |
assetBundleObject = assetBundle; | |
}); | |
while (loadAssetBundleEnumerator.MoveNext()) | |
{ | |
// Spining for Synchronous Behavior (blocking) | |
} | |
return assetBundleObject; | |
} | |
// Asynchronous Method | |
// Download or cache assetbundle to load an object from the assetbundle. | |
public static IEnumerator AsyncLoad<AssetObjectType>(string assetName, string assetBundleURL, int versionNumber, LoadAssetCallback<AssetObjectType> callback) where AssetObjectType : Object | |
{ | |
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 | |
} | |
// Blocking Method (Synchronous) | |
// Download or cache assetbundle to load an object from the assetbundle. | |
public static AssetObjectType Load<AssetObjectType>(string assetName, string assetBundleURL, int versionNumber) where AssetObjectType : Object | |
{ | |
AssetObjectType assetObject = null; | |
IEnumerator loadEnumerator = AsyncLoad<AssetObjectType> (assetName, assetBundleURL, versionNumber, (bool isSuccess, AssetObjectType asset) => | |
{ | |
if( isSuccess == false ) | |
{ | |
assetObject = null; | |
} | |
else | |
{ | |
assetObject = asset; | |
} | |
}); | |
while (loadEnumerator.MoveNext()) | |
{ | |
// Spining for Synchronous(Blocking) Behavior | |
} | |
return assetObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment