免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Unity3d熱更新(三):更新資源流程
1.生成配置文件
在資源打包AssetBundle后,需要計算資源文件的MD5值,生成配置文件。下面給出一個例子:
[csharp] view plaincopy
// 獲取Res文件夾下所有文件的相對路徑和MD5值
string[] files = Directory.GetFiles(resPath, "*", SearchOption.AllDirectories);
StringBuilder versions = new StringBuilder();
for (int i = 0, len = files.Length; i < len; i++)
{
string filePath = files[i];
string extension = filePath.Substring(files[i].LastIndexOf("."));
if (extension == ".unity3d")
{
string relativePath = filePath.Replace(resPath, "").Replace("\\", "/");
string md5 = MD5File(filePath);
versions.Append(relativePath).Append(",").Append(md5).Append("\n");
}
}
// 生成配置文件
FileStream stream = new FileStream(resPath + "version.txt", FileMode.Create);
byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
其中生成MD5值的方法如下:
[csharp] view plaincopy
public static string MD5File(string file)
{
try
{
FileStream fs = new FileStream(file, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fs);
fs.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("md5file() fail, error:" + ex.Message);
}
}
2.版本比較
先加載本地的version.txt,將結(jié)果緩存起來。下載服務器的version.txt,與本地的version進行比較,篩選出需要更新和新增的資源。
3.下載資源
依次下載更新的資源,如果本地已經(jīng)有舊資源,則替換之,否則就新建保存起來。
4.更新本地版本配置文件version.txt
用服務器的version.txt替換掉本地的version.txt。這樣做是為了確保下次啟動的時候,不會再重復更新了。
關于上述的流程,下面有個小例子。這里沒有用到web服務器,而是將本地的另外一個文件夾作為資源服務器目錄。這里的目錄只針對windows下的版本進行測試。如果要在手機平臺上,需要記得更新相關的路徑。
[csharp] view plaincopy
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class ResUpdate : MonoBehaviour
{
public static readonly string VERSION_FILE = "version.txt";
public static readonly string LOCAL_RES_URL = "file://" + Application.dataPath + "/Res/";
public static readonly string SERVER_RES_URL = "file:///C:/Res/";
public static readonly string LOCAL_RES_PATH = Application.dataPath + "/Res/";
private Dictionary LocalResVersion;
private Dictionary ServerResVersion;
private List NeedDownFiles;
private bool NeedUpdateLocalVersionFile = false;
void Start()
{
//初始化
LocalResVersion = new Dictionary();
ServerResVersion = new Dictionary();
NeedDownFiles = new List();
//加載本地version配置
StartCoroutine(DownLoad(LOCAL_RES_URL + VERSION_FILE, delegate(WWW localVersion)
{
//保存本地的version
ParseVersionFile(localVersion.text, LocalResVersion);
//加載服務端version配置
StartCoroutine(this.DownLoad(SERVER_RES_URL + VERSION_FILE, delegate(WWW serverVersion)
{
//保存服務端version
ParseVersionFile(serverVersion.text, ServerResVersion);
//計算出需要重新加載的資源
CompareVersion();
//加載需要更新的資源
DownLoadRes();
}));
}));
}
//依次加載需要更新的資源
private void DownLoadRes()
{
if (NeedDownFiles.Count == 0)
{
UpdateLocalVersionFile();
return;
}
string file = NeedDownFiles[0];
NeedDownFiles.RemoveAt(0);
StartCoroutine(this.DownLoad(SERVER_RES_URL + file, delegate(WWW w)
{
//將下載的資源替換本地就的資源
ReplaceLocalRes(file, w.bytes);
DownLoadRes();
}));
}
private void ReplaceLocalRes(string fileName, byte[] data)
{
string filePath = LOCAL_RES_PATH + fileName;
FileStream stream = new FileStream(LOCAL_RES_PATH + fileName, FileMode.Create);
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}
//顯示資源
private IEnumerator Show()
{
WWW asset = new WWW(LOCAL_RES_URL + "cube.assetbundle");
yield return asset;
AssetBundle bundle = asset.assetBundle;
Instantiate(bundle.Load("Cube"));
bundle.Unload(false);
}
//更新本地的version配置
private void UpdateLocalVersionFile()
{
if (NeedUpdateLocalVersionFile)
{
StringBuilder versions = new StringBuilder();
foreach (var item in ServerResVersion)
{
versions.Append(item.Key).Append(",").Append(item.Value).Append("\n");
}
FileStream stream = new FileStream(LOCAL_RES_PATH + VERSION_FILE, FileMode.Create);
byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}
//加載顯示對象
StartCoroutine(Show());
}
private void CompareVersion()
{
foreach (var version in ServerResVersion)
{
string fileName = version.Key;
string serverMd5 = version.Value;
//新增的資源
if (!LocalResVersion.ContainsKey(fileName))
{
NeedDownFiles.Add(fileName);
}
else
{
//需要替換的資源
string localMd5;
LocalResVersion.TryGetValue(fileName, out localMd5);
if (!serverMd5.Equals(localMd5))
{
NeedDownFiles.Add(fileName);
}
}
}
//本次有更新,同時更新本地的version.txt
NeedUpdateLocalVersionFile = NeedDownFiles.Count > 0;
}
private void ParseVersionFile(string content, Dictionary dict)
{
if (content == null || content.Length == 0)
{
return;
}
string[] items = content.Split(new char[] { '\n' });
foreach (string item in items)
{
string[] info = item.Split(new char[] { ',' });
if (info != null && info.Length == 2)
{
dict.Add(info[0], info[1]);
}
}
}
private IEnumerator DownLoad(string url, HandleFinishDownload finishFun)
{
WWW www = new WWW(url);
yield return www;
if (finishFun != null)
{
finishFun(www);
}
www.Dispose();
}
public delegate void HandleFinishDownload(WWW www);
}
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Unity手游之路<十二>手游資源熱更新策略探討
Unity中資源打包成Assetsbundle的資料整理
[unity3d]保存文件到本地and加載本地文件
Using NAppUpdate to automatically download an...
java 往 pdf 插入數(shù)據(jù) (pdfbox+poi)
加載模塊報錯versionmagicmod
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服