fc2ブログ
 
■プロフィール

クリエイトウェーブ

Author:クリエイトウェーブ
開発における、諸々を書いていきたいなと思っています。
mail : info@createwave.jp

■最近の記事
■カテゴリー
■最近のコメント
■月別アーカイブ

■最近のトラックバック
■ブログ内検索

■RSSフィード
■リンク
[Unity 3D]バイナリファイルの読み書き
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でないと失敗します。


カテゴリー:Unity | TM(0) | CM(0)
コメントの投稿

管理者にだけ表示を許可する