[Unity] File Download 檔案下載

搭配 AssetBundle 可以實現更新 patch 機制
就不用什麼都包進 exe / apk / ipa 裡

// 從 URL 下載
WWW www = new WWW(“http://127.0.0.1/patch/img01.png");
//WWW www = new WWW(“file://“ + Application.persistentDataPath + “/patch/img01.png”);
yield return www;

// 寫入檔案
BinaryWriter Writer = null;
path = Application.persistentDataPath + “/tmp.png”;
if (!File.Exists(path)){
Writer = new BinaryWriter(File.OpenWrite(path));
Writer.Write(www.bytes);
Writer.Flush();
Writer.Close();
}

// 讀取檔案
www = new WWW(“file://“ + Application.persistentDataPath + “/tmp.png”);
yield return www;
// assign the downloaded image to the main texture of the object

_gameObject.renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.ARGB32, false);
//www.LoadImageIntoTexture(_gameObject.renderer.material.mainTexture); Texture2D t = www.texture;

路徑可以參照: [Unity] 各平台檔案路徑 Path

Next: [遊戲開發] Unity Cocos2D 防作弊 遊戲修改大師、八門神器 Prev: [Unity] File Download 檔案下載