unity3d在线更新资源(4)
2015-04-20 17:00
141 查看
经过一天的尝试,终于确认方案可行,主要代码如下:
using UnityEngine;
using System.IO;
using System.Threading;
using SevenZip;
using Awesome.Net.Compression;
using System.Collections;
using System.Collections.Generic;
public class VersionCheckControl:MonoBehaviour
{
ProgressBar bar;
string[] path;
string dic;
UILabel progress;
UILabel checkVersionLabel;
Dictionary<string, bool> compressDic = new Dictionary<string, bool>();
void Awake()
{
bar = transform.Find ("Progress2").GetComponent<ProgressBar> ();
progress = bar.transform.Find ("Label").GetComponent<UILabel> ();
UILabel label = bar.transform.Find ("Tip").GetComponent<UILabel> ();
label.text = Localization.Get ("810");
checkVersionLabel = transform.Find ("CheckVersionLabel").GetComponent<UILabel> ();
checkVersionLabel.text = Localization.Get ("811");
bar.gameObject.SetActive (false);
}
void Start()
{
StartCoroutine (downLoadVersionFile ());
}
string serverVersion;
string localPath;
string assetVersionStr;
string localAssetPath;
List<string> needDownList = new List<string>();
IEnumerator downLoadVersionFile()
{
WWW versionWWW = new WWW (PlatformConfig.ServerVersionPath() + "Version");
yield return versionWWW;
if(versionWWW.error != null)
{
Debug.Log(versionWWW.error);
checkVersionLabel.text = Localization.Get ("812");
}
else
{
serverVersion = StringUtil.removeHuiChe(versionWWW.text);
localPath = Application.persistentDataPath + "/Version";
if(File.Exists(localPath))
{
string localVersion = File.ReadAllText(localPath);
if(localVersion == serverVersion)
{
checkVersionLabel.gameObject.SetActive(false);
startConnect();
}
else
{
StartCoroutine(DownLoadAssetVersionFile());
}
}
else
{
StartCoroutine(DownLoadAssetVersionFile());
}
}
}
IEnumerator DownLoadAssetVersionFile()
{
WWW assetVersion = new WWW(PlatformConfig.ServerVersionPath() + "AssetVersion");
yield return assetVersion;
if(assetVersion.error != null)
{
File.WriteAllText(localPath, serverVersion);
checkVersionLabel.gameObject.SetActive(false);
startConnect();
}
else
{
assetVersionStr = StringUtil.removeHuiChe(assetVersion.text);
localAssetPath = Application.persistentDataPath + "/AssetVersion";
Dictionary<string, int> localAsset = new Dictionary<string, int>();
if(File.Exists(localAssetPath))
{
string localAssetStr = File.ReadAllText(localAssetPath);
string[] localAssetArr = localAssetStr.Split('|');
for(int i = 0; i < localAssetArr.Length; i++)
{
string[] arr = localAssetArr[i].Split('-');
localAsset.Add(arr[0], int.Parse(arr[1]));
}
}
string[] serverAssetArr = assetVersionStr.Split('|');
for(int i = 0; i < serverAssetArr.Length; i++)
{
string[] arr = serverAssetArr[i].Split('-');
string key = arr[0];
int serverAssetVersion = int.Parse(arr[1]);
if(!localAsset.ContainsKey(key) || localAsset[key] < serverAssetVersion)
{
needDownList.Add(key);
}
}
StartCoroutine(DownLoadBundleFile());
}
}
WWW bundle;
int index = 0;
bool isAllDone = false;
IEnumerator DownLoadBundleFile()
{
checkVersionLabel.gameObject.SetActive (false);
if(needDownList.Count <= 0)
{
File.WriteAllText(localPath, serverVersion);
startConnect();
}
else
{
bar.gameObject.SetActive(true);
for(int i = 0; i < needDownList.Count; i++)
{
bundle = new WWW(PlatformConfig.ServerVersionPath() + needDownList[i] + ".assetBundle");
yield return bundle;
if(bundle.error != null)
{
Debug.Log(bundle.error);
}
else
{
File.WriteAllBytes(PlatformConfig.PathOfArchive() + needDownList[i] + ".assetBundle", bundle.bytes);
}
index++;
}
File.WriteAllText(localAssetPath, assetVersionStr);
File.WriteAllText(localPath, serverVersion);
isAllDone = true;
}
}
void Update()
{
if(bar.gameObject.activeSelf)
{
bar.setValue (bundle.progress / needDownList.Count + index / needDownList.Count, true);
progress.text = Mathf.CeilToInt(bar.getValue() * 100).ToString() + "%";
if(isAllDone && bar.getValue() >= 1f)
{
startConnect();
isAllDone = false;
}
}
}
void startConnect()
{
bar.gameObject.SetActive (false);
ResManager.getInst ().preLoadBundle ();
LoginAction action = GetComponent<LoginAction>();
action.startConnect();
}
}
整体思路还是很简单的,就是先把流程走通。我也觉得目前的方案应该是最好的选择。
using UnityEngine;
using System.IO;
using System.Threading;
using SevenZip;
using Awesome.Net.Compression;
using System.Collections;
using System.Collections.Generic;
public class VersionCheckControl:MonoBehaviour
{
ProgressBar bar;
string[] path;
string dic;
UILabel progress;
UILabel checkVersionLabel;
Dictionary<string, bool> compressDic = new Dictionary<string, bool>();
void Awake()
{
bar = transform.Find ("Progress2").GetComponent<ProgressBar> ();
progress = bar.transform.Find ("Label").GetComponent<UILabel> ();
UILabel label = bar.transform.Find ("Tip").GetComponent<UILabel> ();
label.text = Localization.Get ("810");
checkVersionLabel = transform.Find ("CheckVersionLabel").GetComponent<UILabel> ();
checkVersionLabel.text = Localization.Get ("811");
bar.gameObject.SetActive (false);
}
void Start()
{
StartCoroutine (downLoadVersionFile ());
}
string serverVersion;
string localPath;
string assetVersionStr;
string localAssetPath;
List<string> needDownList = new List<string>();
IEnumerator downLoadVersionFile()
{
WWW versionWWW = new WWW (PlatformConfig.ServerVersionPath() + "Version");
yield return versionWWW;
if(versionWWW.error != null)
{
Debug.Log(versionWWW.error);
checkVersionLabel.text = Localization.Get ("812");
}
else
{
serverVersion = StringUtil.removeHuiChe(versionWWW.text);
localPath = Application.persistentDataPath + "/Version";
if(File.Exists(localPath))
{
string localVersion = File.ReadAllText(localPath);
if(localVersion == serverVersion)
{
checkVersionLabel.gameObject.SetActive(false);
startConnect();
}
else
{
StartCoroutine(DownLoadAssetVersionFile());
}
}
else
{
StartCoroutine(DownLoadAssetVersionFile());
}
}
}
IEnumerator DownLoadAssetVersionFile()
{
WWW assetVersion = new WWW(PlatformConfig.ServerVersionPath() + "AssetVersion");
yield return assetVersion;
if(assetVersion.error != null)
{
File.WriteAllText(localPath, serverVersion);
checkVersionLabel.gameObject.SetActive(false);
startConnect();
}
else
{
assetVersionStr = StringUtil.removeHuiChe(assetVersion.text);
localAssetPath = Application.persistentDataPath + "/AssetVersion";
Dictionary<string, int> localAsset = new Dictionary<string, int>();
if(File.Exists(localAssetPath))
{
string localAssetStr = File.ReadAllText(localAssetPath);
string[] localAssetArr = localAssetStr.Split('|');
for(int i = 0; i < localAssetArr.Length; i++)
{
string[] arr = localAssetArr[i].Split('-');
localAsset.Add(arr[0], int.Parse(arr[1]));
}
}
string[] serverAssetArr = assetVersionStr.Split('|');
for(int i = 0; i < serverAssetArr.Length; i++)
{
string[] arr = serverAssetArr[i].Split('-');
string key = arr[0];
int serverAssetVersion = int.Parse(arr[1]);
if(!localAsset.ContainsKey(key) || localAsset[key] < serverAssetVersion)
{
needDownList.Add(key);
}
}
StartCoroutine(DownLoadBundleFile());
}
}
WWW bundle;
int index = 0;
bool isAllDone = false;
IEnumerator DownLoadBundleFile()
{
checkVersionLabel.gameObject.SetActive (false);
if(needDownList.Count <= 0)
{
File.WriteAllText(localPath, serverVersion);
startConnect();
}
else
{
bar.gameObject.SetActive(true);
for(int i = 0; i < needDownList.Count; i++)
{
bundle = new WWW(PlatformConfig.ServerVersionPath() + needDownList[i] + ".assetBundle");
yield return bundle;
if(bundle.error != null)
{
Debug.Log(bundle.error);
}
else
{
File.WriteAllBytes(PlatformConfig.PathOfArchive() + needDownList[i] + ".assetBundle", bundle.bytes);
}
index++;
}
File.WriteAllText(localAssetPath, assetVersionStr);
File.WriteAllText(localPath, serverVersion);
isAllDone = true;
}
}
void Update()
{
if(bar.gameObject.activeSelf)
{
bar.setValue (bundle.progress / needDownList.Count + index / needDownList.Count, true);
progress.text = Mathf.CeilToInt(bar.getValue() * 100).ToString() + "%";
if(isAllDone && bar.getValue() >= 1f)
{
startConnect();
isAllDone = false;
}
}
}
void startConnect()
{
bar.gameObject.SetActive (false);
ResManager.getInst ().preLoadBundle ();
LoginAction action = GetComponent<LoginAction>();
action.startConnect();
}
}
整体思路还是很简单的,就是先把流程走通。我也觉得目前的方案应该是最好的选择。
相关文章推荐
- unity3d 在线更新资源(3)
- unity3d在线更新资源专题(6)
- unity3d在线更新资源(5)
- unity3d在线更新资源(2)
- Android SDK 在线更新镜像服务器资源
- Android SDK 在线更新镜像服务器资源
- 学习苹果Swift语言的一些在线资源(2014年9月10日更新)
- 【Unity3d_常用模块】客户端资源版本更新模块(资源热更新)
- Unity3d热更新全书-资源加载(一):从AssetBundle说起
- Unity3d的热更新和资源下载
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- 函数项目【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)函数项目
- Android SDK 在线更新镜像服务器资源
- unity3d的资源管理和热更新方案
- Unity3D 学习笔记7 —— 获取热更新资源文件的MD5码
- Android SDK 在线更新镜像服务器资源
- 利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- Android SDK 在线更新镜像服务器资源
- 基于Keystone 架构的高性能多核C66x系列DSP在线资源汇总(持续更新中)
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)