Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AssetBundleを使うサンプル
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour
{
void Start ()
{
// Clear Cache
Caching.CleanCache();
#if UNITY_ANDROID && !UNITY_EDITOR
string url = "https://dl.dropboxusercontent.com/s/cc0lem0b9xrnf3h/Asset.unity3d.android.unity3d?dl=1&token_hash=AAGn46HI9bNZuksK-Pk8irAps0PJ5QER0NM_V2MqaBL49g";
#elif UNITY_IPHONE && !UNITY_EDITOR
string url = "https://dl.dropboxusercontent.com/s/lp45cpfiv878gv0/Asset.unity3d.iphone.unity3d?dl=1&token_hash=AAHeyEv5Bf5YjbMfN_1sqCWayBKpFq_DdOMdKbHXm2Rv7w";
#else
string url = "https://dl.dropboxusercontent.com/s/xfo6raqkrevuyyv/Asset.unity3d.unity3d?dl=1&token_hash=AAFDzt8IplClDP9oozDHFIhgrdnEgXwASEMY1i-cF6MgYg";
#endif
StartCoroutine (DownloadAndCache ("Particle System",
url,
1));
StartCoroutine (DownloadAndCache ("Sprite",
url,
1));
}
public IEnumerator DownloadAndCache (string assetName, string url, int version = 1)
{
// キャッシュシステムの準備が完了するのを待ちます
while (!Caching.ready)
yield return null;
// 同じバージョンが存在する場合はアセットバンドルをキャッシュからロードするか、またはダウンロードしてキャッシュに格納します。
using (WWW www = WWW.LoadFromCacheOrDownload (url, version)) {
yield return www;
if (www.error != null) {
throw new Exception ("WWWダウンロードにエラーがありました:" + www.error);
}
AssetBundle bundle = www.assetBundle;
if (assetName == "")
Instantiate (bundle.mainAsset);
else
Instantiate (bundle.Load (assetName));
// メモリ節約のため圧縮されたアセットバンドルのコンテンツをアンロード
bundle.Unload (false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
Debug.Log(Caching.IsVersionCached(url, 1));
Debug.Log("DownloadAndCache end");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment