Last active
February 24, 2016 10:17
-
-
Save memememomo/a43c2b35624e4f56e51d to your computer and use it in GitHub Desktop.
AssetBundleのサンプルを作成した時のメモ ref: http://qiita.com/uchiko/items/91de62f4cc2d243b479f
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
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); | |
} | |
} | |
} |
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
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); | |
} | |
} |
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
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