Skip to content

Instantly share code, notes, and snippets.

@memememomo
Last active February 24, 2016 10:17
Show Gist options
  • Save memememomo/a43c2b35624e4f56e51d to your computer and use it in GitHub Desktop.
Save memememomo/a43c2b35624e4f56e51d to your computer and use it in GitHub Desktop.
AssetBundleのサンプルを作成した時のメモ ref: http://qiita.com/uchiko/items/91de62f4cc2d243b479f
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour
{
string bundleURL = "http://localhost:9000/assets/StreamingAssets/sprites";
string assetName = "Test";
int version = 0;
void Start()
{
StartCoroutine(DownloadAndCache());
}
IEnumerator DownloadAndCache()
{
while(!Caching.ready)
yield return null;
using(WWW www = WWW.LoadFromCacheOrDownload(bundleURL, version)) {
yield return www;
if (www.error != null) {
throw new UnityException("WWW download had an error" + www.error);
}
AssetBundle bundle = www.assetBundle;
Instantiate(bundle.LoadAsset(assetName));
bundle.Unload(false);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class ExportAssetBundle
{
[MenuItem("Export/AssetBundle")]
static void Export()
{
Directory.CreateDirectory(Application.streamingAssetsPath);
BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath);
}
}
using UnityEngine;
using System.Collections;
public class LoadAssetBundle : MonoBehaviour
{
string assetName = "Test";
string AssetPath {
get {
return Application.streamingAssetsPath + "/sprites";
}
}
IEnumerator Start()
{
var resultAssetBundle = AssetBundle.LoadFromFileAsync(AssetPath);
yield return new WaitWhile(() => resultAssetBundle.isDone == false);
var assetbundle = resultAssetBundle.assetBundle;
var resultObject = assetbundle.LoadAssetAsync<GameObject>(assetName);
yield return new WaitWhile(() => resultObject.isDone == false);
GameObject.Instantiate(resultObject.asset);
assetbundle.Unload(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment