投稿日:2012-03-13 Tue
Pです。WWW.LoadFromCacheOrDownload()は、何かと使いにくい面があるのでこれに似たサンプルを作ってみました。
※WebPlayerでは使用できないので注意!
参考にしたページ
http://forum.unity3d.com/threads/76852-asset-bundle-downloading-and-saving-to-device
using UnityEngine;
using System.Collections;
using System.IO;
public class LoadFromCacheOrDownloadTest : MonoBehaviour {
IEnumerator LoadHTTP() {
string url = "http://xxxx/xx.unity3d";
WWW www = new WWW(url);
yield return www;
if (www.error == null) {
string path = Application.persistentDataPath + "/xx.unity3d";
File.WriteAllBytes(path, www.bytes);
}
}
IEnumerator LoadLocal() {
string url = "file://" + Application.persistentDataPath + "/xx.unity3d";
WWW www = new WWW(url);
yield return www;
if (www.error == null)
AssetBundle ab = www.assetBundle;
}
void OnGUI() {
if (GUI.Button(new Rect(20, 20, 200, 60), "Load from http"))
StartCoroutine("LoadHTTP");
if (GUI.Button(new Rect(20, 100, 200, 60), "Load from cache"))
StartCoroutine("LoadLocal");
}
}
File.WriteAllBytes()で書き込む時は「Application.persistentDataPath」
ローカルキャッシュから読み込む時は「"file://" + Application.persistentDataPath」
というstringでないと失敗します。
△ PAGE UP