/// 获取资源路径
/// </summary>
public static string GetAssetPath ()
{
string bundlePath =
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
"file://" + Application.streamingAssetsPath;
#elif UNITY_IPHONE || UNITY_IOS || UNITY_IPHONE_API
"file://" + Application.streamingAssetsPath;
#elif UNITY_STANDALONE_WIN
"file://" +Application.streamingAssetsPath;
#elif UNITY_WEBPLAYER
"file://" +Application.streamingAssetsPath;
#elif UNITY_ANDROID
Application.streamingAssetsPath;
#endif
return bundlePath;
}
//获取动态更新的目录
public static string GetDocumentsPath ()
{
string doucmPath =
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
"file://" + Application.persistentDataPath + "/Res";
#elif UNITY_IPHONE || UNITY_IOS || UNITY_IPHONE_API
"file://" + Application.persistentDataPath + "/Res";
#elif UNITY_STANDALONE_WIN
"file://" +Application.persistentDataPath;
#elif UNITY_WEBPLAYER
"file://" +Application.persistentDataPath ;
#elif UNITY_ANDROID
Application.persistentDataPath;
#endif
//"file://" + Application.persistentDataPath ;
GLogger.Log ("==========documents " + doucmPath);
return doucmPath;
}
public static string GetStreamingAssetsPath ()
{
string path =
#if UNITY_EDITOR || UNITY_STANDALONE_OSX
// "file://" + Application.streamingAssetsPath + "/";
"file://" + Application.streamingAssetsPath;
#elif UNITY_IPHONE || UNITY_IOS || UNITY_IPHONE_API
"file://" + Application.streamingAssetsPath + "/Res";
#elif UNITY_STANDALONE_WIN
"file://" +Application.streamingAssetsPath;
#elif UNITY_WEBPLAYER
"file://" +Application.streamingAssetsPath ;
#elif UNITY_ANDROID
Application.streamingAssetsPath ;
#endif
// "file://" + Application.streamingAssetsPath ;
GLogger.Log ("==========documents " + path);
return path;
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class ResourcesUpdate : MonoBehaviour
{
private static ResourcesUpdate instance;
public static ResourcesUpdate GetInstance ()
{
return instance;
}
//本地存储版本号
public static readonly string LOCAL_VERSION_FILE = "/LocalVersion.txt";
public static readonly string LOCAL_ALl_PREFAB = "/LocalResources/ALL.assetbundle";
public static readonly string LOCAL_ALl_PERSISTENT_PREFAB = "/LOCAL_ALL.assetbundle";
public static readonly string LOCAL_PERSISTENT_VERSION_FILE = "/LOCAL_Version.txt";
//服务器
public static readonly string SERVER_VERSION_FILE = "/ServerVersion.txt";
public static readonly string SERVER_ALl_PREFAB = "/ServerResources/ALL.assetbundle";
public static string LOCAL_RES_PATH = "";
public static string LOCAL_RES_URL = "";
public static string SERVER_RES_URL = "";
private List<string> LocalResVersion;
private List<string> ServerResVersion;
public delegate void HandleFinishDownload (WWW www);
public delegate void DownloadFinishResources ();
public DownloadFinishResources finishResources;
public AssetBundle bundle;
private bool isNeedUpdate = false;
private int codeVersion;
private int localResVersion;
private int serverResVersion;
public bool isLoadingResources = false;
// public GameObject setObjectPoolActive;
void Awake ()
{
//保存到本地持久化目录
LOCAL_RES_PATH = GameUtils.GetDocumentsPath ();// + "/"
LOCAL_RES_URL = GameUtils.GetStreamingAssetsPath ();
SERVER_RES_URL = GameUtils.GetStreamingAssetsPath ();
instance = this;
}
void Start ()
{
LocalResVersion = new List<string> ();
ServerResVersion = new List<string> ();
if (bundle != null) {
return;
} else {
LoadLocalVersion ();
}
}
public void LoadLocalVersion ()
{
//加载本地version配置
StartCoroutine (DownLoad (LOCAL_RES_URL + LOCAL_VERSION_FILE, delegate(WWW localVersion) {
// GLogger.LogWarning("本地文件路径名: " + LOCAL_RES_URL + LOCAL_VERSION_FILE);
//保存本地的version
ParseLocalVersionFile (localVersion.text);
//加载服务端version配置
StartCoroutine (this.DownLoad (SERVER_RES_URL + SERVER_VERSION_FILE, delegate(WWW serverVersion) {
// GLogger.LogWarning("服务器文件路径名: " + LOCAL_RES_URL + LOCAL_VERSION_FILE);
//保存服务端version
ParseServerVersionFile (serverVersion.text);
//是否需要重新加载资源
CompareVersion ();
//加载需要更新的资源
DownLoadRes ();
}));
}));
}
/// <summary>
/// 读取streamingAssetsPath下的Version文件到PersistentDataPath下
/// </summary>
void SaveVersionToPersistentDataPath (string localVersion)
{
FileStream stream;
string filePath = LOCAL_RES_PATH.Replace ("file://", "") + LOCAL_PERSISTENT_VERSION_FILE;
if (File.Exists (filePath)) {
stream = new FileStream (filePath, FileMode.Create);
} else {
CreateDic (filePath);
stream = File.Create (filePath);
}
// FileStream stream = new FileStream (filePath, FileMode.Create);
byte[] data = Encoding.UTF8.GetBytes (localVersion);
stream.Write (data, 0, data.Length);
stream.Flush ();
stream.Close ();
}
private IEnumerator DownLoad (string url, HandleFinishDownload finishFun)
{
WWW www = new WWW (url);
yield return www;
if (www.isDone) {
if (finishFun != null) {
finishFun (www);
}
}
www.Dispose ();
}
private void ParseLocalVersionFile (string content)
{
if (content == null || content.Length == 0) {
return;
}
string[] items = content.Split (new char[] { '\n' });
foreach (string item in items) {
LocalResVersion.Add (item);
}
ParseVersion.GetInstance ().LoadVersion (content);
localResVersion = ParseVersion.GetInstance ().GetResVersionNum ();
}
private void ParseServerVersionFile (string content)
{
if (content == null || content.Length == 0) {
return;
}
string[] items = content.Split (new char[] { '\n' });
foreach (string item in items) {
ServerResVersion.Add (item);
}
ParseVersion.GetInstance ().LoadVersion (content);
serverResVersion = ParseVersion.GetInstance ().GetResVersionNum ();
}
private void CompareVersion ()
{
if (localResVersion != serverResVersion) {
GLogger.LogWarning ("资源版本不相等!!!");
isNeedUpdate = true;
} else {
isNeedUpdate = false;
GLogger.LogWarning ("资源版本相等!!!");
}
}
//依次加载需要更新的资源
private void DownLoadRes ()
{
if (!isNeedUpdate) {
LoadAndShowAssetBundle ();
return;
}
UpdateLocalVersionFile ();
StartCoroutine (this.DownLoad (SERVER_RES_URL + SERVER_ALl_PREFAB, delegate(WWW w) {
//将下载的资源替换本地旧的资源
ReplaceLocalRes (SERVER_ALl_PREFAB, w.bytes);
isNeedUpdate = false;
DownLoadRes ();
}));
}
void LoadAndShowAssetBundle ()
{
StartCoroutine (Show ());
}
//显示资源
private IEnumerator Show ()
{
isLoadingResources = true;
GLogger.LogWarning ("正在加载资源...");
// WWW asset = new WWW (LOCAL_RES_PATH + LOCAL_ALl_PERSISTENT_PREFAB);
WWW asset = new WWW (LOCAL_RES_URL + LOCAL_ALl_PREFAB);
GLogger.LogWarning ("=================本地资源路径名=================: " + (LOCAL_RES_PATH + LOCAL_ALl_PERSISTENT_PREFAB));
yield return asset;
if (asset.isDone) {
bundle = asset.assetBundle;
SavePrefabDllToPersistentDataPath (asset.text);
isLoadingResources = false;
}
if(finishResources != null){
finishResources ();
}
}
/// <summary>
/// 保存本地资源dll到持久化目录
/// </summary>
/// <param name="prefabDll">Prefab dll.</param>
void SavePrefabDllToPersistentDataPath (string prefabDll)
{
FileStream stream;
string filePath = LOCAL_RES_PATH.Replace ("file://", "") + LOCAL_ALl_PERSISTENT_PREFAB;
if (File.Exists (filePath)) {
stream = new FileStream (filePath, FileMode.Create);
} else {
CreateDic (filePath);
stream = File.Create (filePath);
}
byte[] data = Encoding.UTF8.GetBytes (prefabDll);
stream.Write (data, 0, data.Length);
stream.Flush ();
stream.Close ();
}
//更新本地的version配置
private void UpdateLocalVersionFile ()
{
if (isNeedUpdate) {
StringBuilder versions = new StringBuilder ();
foreach (var item in ServerResVersion) {
versions.Append (item).Append ("\n");
}
GLogger.LogWarning ("====================更新LocalVersion!");
FileStream stream;
string filePath = LOCAL_RES_PATH.Replace ("file://", "") + LOCAL_PERSISTENT_VERSION_FILE;
if (File.Exists (filePath)) {
stream = new FileStream (filePath, FileMode.Create);
} else {
CreateDic (filePath);
stream = File.Create (filePath);
}
// FileStream stream = new FileStream (LOCAL_RES_PATH.Replace ("file://", "") + LOCAL_PERSISTENT_VERSION_FILE, FileMode.Create);
byte[] data = Encoding.UTF8.GetBytes (versions.ToString ());
stream.Write (data, 0, data.Length);
stream.Flush ();
stream.Close ();
}
// setObjectPoolActive.SetActive (true);
}
private void ReplaceLocalRes (string fileName, byte[] data)
{
FileStream stream;
string filePath = LOCAL_RES_PATH.Replace ("file://", "") + LOCAL_ALl_PERSISTENT_PREFAB;
if (File.Exists (filePath)) {
stream = new FileStream (filePath, FileMode.Create);
} else {
CreateDic (filePath);
stream = File.Create (filePath);
}
// FileStream stream = new FileStream (filePath, FileMode.Create);
stream.Write (data, 0, data.Length);
stream.Flush ();
stream.Close ();
}
//创建路径
void CreateDic (string filePath)
{
int index = filePath.LastIndexOf ('/');
string dir = filePath.Substring (0, index + 1);
if (!Directory.Exists (dir)) {
Directory.CreateDirectory (dir);
}
}
}