2014年2月26日 星期三

Unity AssetBundle 取用 C#

1.事前工作。
 1.1遠端準備一份.csv打包成.AssetBundle的檔案,內容欄位名,版本名。
 1.2本機端建立一資料夾AssetBundle,放入同為1.1的.AssetBundle。
 1.3再建立一資料夾StreamingAssets(必需為此名),裡面存放另一份Local.csv檔。

2範例流程
  2.1從遠端位置VersionURL 取得 AssetBundle,內容為資源名+資源版號。
  2.2轉成binAsset ,將內容存入VersionArray_Server二維字串陣列。
  2.3各資源利用LoadFromCacheOrDownload()方法,填入路徑及取得的版本號,即自行會判斷 更新,也就是透過此機制,可動態更新使用者端的表格資料及資源的AssetBundle,但用於手 機的AssetBundle,資源中不可含Script(此部份為網路上文章說明,個人未實測證明)。

 2.4以上的資料是在建立於手機本機端的快取區作業。

 2.5在分隔線後面部份,是取出位於打包好的檔案中,類似Android的.apk檔中,也就是步驟1.3 所存放的Local.csv檔,使用Application.persistentDataPath()方法,在手機本體中指定路徑,產生 一個新的檔案,此檔案可進行修改的操作。

2.6取得資源型的AssetBundle,動態生成至Scence的範例方法如下:
     Instantiate(bundle.Load("Cube_R"));


3.於Unity Assets下建立Script,視需求將此Script置於物件中,範例是放在Main Camera下,程式碼如下。

using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;  
using System.Text; 


private string VersionURL =連結目標自設";
private string BundleURL = "連結目標自設";
private string SceneURL = "連結目標自設";


private string[][] VersionArray_Server;
string UIVersion,CharacterVersion,MonsterVersion,SceneVersion,EquipmentVersion,TableVersion;
bool DownloadOK;
string WeaponTableVersion = "";
string LocalFileVersion ="";
string DBPath = "";
string filepath = "";


void Start()
{
DownloadOK = false;
//在此決定要向儲存體,下載那個版本

//取得+比對版本
StartCoroutine(DownloadVersion_Sever(VersionURL,"Version",BundleURL));
}

//取得伺服器表格    版本連結  檔名  表格連結
IEnumerator DownloadVersion_Sever(string VersionURL_Server ,string FileName ,string TableURL)
{

//更新版本表格
using (WWW asset = new WWW(VersionURL_Server))
{
yield return asset;
AssetBundle bundle = asset.assetBundle;

//bundle轉TextAsset 二進位文件
TextAsset binAsset = bundle.Load ("Version", typeof(TextAsset)) as TextAsset;

if(binAsset == null)
{
//沒取得版號,或內容不完整
Debug.LogError("VersionURL" + "no binAsset!!");
}
else
{
//讀取資料內容,遇到資料結尾\r,判定為一筆
string [] lineArray = binAsset.text.Split ("\r"[0]);

//建立存放陣列,依資料筆數,決定欄位數量
VersionArray_Server = new string [lineArray.Length][];

//把csv中的的數據填入陣列中 
for(int i =0;i < lineArray.Length; i++)  
{  
VersionArray_Server[i] = lineArray[i].Split (',');  
}
}
Debug.Log("Server端資料:" + GetDataByRowAndCol(0,0,VersionArray_Server)+ "+" +GetDataByRowAndCol(0,1,VersionArray_Server));
bundle.Unload(false);
}

//武器表格
using (WWW asset = WWW.LoadFromCacheOrDownload(TableURL, int.Parse(GetDataByRowAndCol(0,1,VersionArray_Server)))) //下載地址,版本號
{
yield return asset;

AssetBundle bundle = asset.assetBundle;

//bundle轉TextAsset 二進位文件
TextAsset binAsset = bundle.Load ("Weapon", typeof(TextAsset)) as TextAsset;

WeaponTableVersion = binAsset.text;

Debug.Log("已更換"+TableURL+"版本");
bundle.Unload(false);
}

//------------------------------------------------------------------------------------------------------------------------------------------------------//

//各平台路徑
#if UNITY_EDITOR
filepath = Application.dataPath +"/StreamingAssets/Local.csv";
#elif UNITY_ANDROID
filepath = "jar:file://" + Application.dataPath + "!/assets/Local.csv";
#elif UNITY_IPHONE
filepath = Application.dataPath +"/Raw"+"/Local.csv";
#else
filepath = Application.dataPath +"/StreamingAssets/Local.csv";
#endif
string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, "Local.csv");


//判定平台
if(Application.platform == RuntimePlatform.IPhonePlayer)
{
using (WWW reader = new WWW(filepath))
{
yield return reader;
LocalFileVersion = reader.text;
}
}
else if(Application.platform == RuntimePlatform.Android)
{
using (WWW reader = new WWW(filepath))
{
//讀出
yield return reader;
LocalFileVersion = reader.text;

//寫入
string WritePath = Application.persistentDataPath + "/Local";
System.IO.File.WriteAllBytes(WritePath, reader.bytes);
DBPath = WritePath;
}
}
else
{
// pc
LocalFileVersion = System.IO.File.ReadAllText(oriPath);
}
DownloadOK = true;
}

void OnGUI()
{
if(DownloadOK)
{
GUI.Label (new Rect(Screen.width -700, Screen.height - 250,500,20),"伺服器版本號為:"+GetDataByRowAndCol(0,1,VersionArray_Server));
GUI.Label (new Rect(Screen.width -700, Screen.height - 200,500,20),"本機端版本號為:"+WeaponTableVersion);
GUI.Label (new Rect(Screen.width -700, Screen.height - 150,500,20),"本機端CSV檔版本號為:"+LocalFileVersion);
GUI.Label (new Rect(Screen.width -700, Screen.height - 100,500,20),"本機端檔案路徑為:"+DBPath);
GUI.Label (new Rect(Screen.width -800, Screen.height - 50,500,20),"filepath路俓為:"+filepath);
}
}


//下载Scene,加载场景  
IEnumerator DownloadScene()
{
//下载Scene,加载场景
using (WWW scene = WWW.LoadFromCacheOrDownload(SceneURL,1)) //下載地址,版本號
//using (WWW scene = new WWW(SceneURL))
{
yield return scene;
AssetBundle bundleS = scene.assetBundle;

if(bundleS == null)
{
Debug.LogError("no Scene");
}

Application.LoadLevel("scene1");
}
}


//取得指定資料
string GetDataByRowAndCol(int nRow, int nCol, string[][] CSVArray)  
{  
if (CSVArray.Length <= 0 || nRow >= CSVArray.Length)  
return "";  
if (nCol >= CSVArray[0].Length)  
return "";  

return CSVArray[nRow][nCol];  
}

}


沒有留言:

張貼留言