Unity更新资源代码

Posted what-lee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity更新资源代码相关的知识,希望对你有一定的参考价值。

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

public class ResUpdate : MonoBehaviour
{
public string VERSION_FILE;// = "version.txt"; //文件名
public string LOCAL_RES_URL;// = "file://" + Application.dataPath + "/Res/"; //本地文件的路径
public string SERVER_RES_URL;// = "file:///C:/Res/"; //服务器文件的路径
public string LOCAL_RES_PATH;// = Application.dataPath + "/Res/"; //游戏文件的本地路径

private Dictionary<string,string> LocalResVersion;

private Dictionary<string, string> ServerResVersion;
private List<string> NeedDownFiles; //需要更新的文件列表
private bool NeedUpdateLocalVersionFile = false; //是否需要更新

private void Awake()
{
NeedDownFiles = new List<string>();
VERSION_FILE = "version.txt";
LOCAL_RES_URL = "file://" + Application.dataPath + "/Res/";
SERVER_RES_URL = @"file://C:/Users/Administrator/Desktop/";
LOCAL_RES_PATH = Application.dataPath + "/Res/";
}

void Start()
{
//初始化
LocalResVersion = new Dictionary<string, string>();
ServerResVersion = new Dictionary<string, string>();
//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()
{
//yield return null;
WWW asset = new WWW(LOCAL_RES_URL + "cube.assetbundle");
yield return asset;
AssetBundle bundle = asset.assetBundle;
Instantiate(bundle.LoadAsset("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(" ");
}

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); //从本地文件判断一下服务器中这个文件的名字的MD5 并返回
if (!serverMd5.Equals(localMd5)) //如果服务器中这个MD5不等于本地的
{
NeedDownFiles.Add(fileName); //添加到集合
}
}
}
//本次有更新,同时更新本地的version.txt
NeedUpdateLocalVersionFile = NeedDownFiles.Count > 0; //判断需要更新和添加的资源是否大于0 赋值给bool判断变量
}

private void ParseVersionFile(string content, Dictionary<string, string> dict)
{
if (content == null || content.Length == 0)
{
return;
}
string[] items = content.Split(new char[] { ‘ ‘ });
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);
}







































































































































































以上是关于Unity更新资源代码的主要内容,如果未能解决你的问题,请参考以下文章

unity 代码热更+资源管理框架总结

最新完整热更新实战案例学习,包括资源热更新及代码热更新文末送书

为啥Unity没有实现iOS平台代码热更新?

unity引擎热更新流程(主要是资源加载方面)--unity+tolua

Unity资源热更新--资源管理Addressable

Unity 大版本更新之APK的下载与覆盖安装