Exemple #1
0
 public bool ExeCommand(CommandEnum command)
 {
     if (command == CommandEnum.UpdateRemoteAssetBundle)
     {
         if (Config.UseAssetBundle)
         {
             Debug.Log("开始下载资源");
             StartCoroutine(CheckAndDownloadAssetBundle());
         }
         else
         {
             MessageCenter.Dispatch(MsgEnum.ABLoadingFinish);
             CommandController.Instance.Execute(CommandEnum.StartLuaMain);
         }
         return(true);
     }
     return(false);
 }
Exemple #2
0
        /// <summary>
        /// 启动更新下载,这里只是个思路演示,此处可启动线程下载更新
        /// </summary>
        IEnumerator CheckAndDownloadAssetBundle()
        {
            MessageCenter.Dispatch(MsgEnum.ABLoadingBegin);
            MessageCenter.Dispatch(MsgEnum.ABLoadingProgress, 0);
            //读取本地MD5文件
            localFiles = new Dictionary <string, string>();
            string localFilesPath = LuaConst.localABPath + "/" + LuaConst.MD5FileName;

            Debug.Log("localFilesPath: " + localFilesPath);
            if (File.Exists(localFilesPath))
            {
                localFiles = ParseKeyValue(File.ReadAllText(localFilesPath));
            }
            //下载远程MD5文件
            string          filesUrl = Config.RemoteUrl + "/" + LuaConst.MD5FileName;
            UnityWebRequest request  = UnityWebRequest.Get(filesUrl);

            yield return(request.SendWebRequest());

            if (request.error != null)
            {
                Debug.LogError("[ " + filesUrl + " ]" + request.error);
                yield break;
            }
            if (!Directory.Exists(LuaConst.localABPath))
            {
                Directory.CreateDirectory(LuaConst.localABPath);
            }
            Dictionary <string, string> newestFiles = ParseKeyValue(request.downloadHandler.text);
            Dictionary <string, string> reloadFiles = new Dictionary <string, string>();

            foreach (var item in newestFiles)
            {
                bool localHaveKey = localFiles.ContainsKey(item.Key);
                if (!localHaveKey)
                {
                    //是新的文件,加入加载
                    reloadFiles.Add(item.Key, item.Value);
                }
                else
                {
                    bool fileExists = File.Exists(LuaConst.localABPath + "/" + item.Key);
                    if (!fileExists)
                    {
                        //本地找不到,加入下载
                        reloadFiles.Add(item.Key, item.Value);
                    }
                    else
                    {
                        string localHash  = localFiles[item.Key];
                        string remoteHash = newestFiles[item.Key];
                        bool   md5Match   = localHash.Equals(remoteHash);
                        if (!md5Match)
                        {
                            //文件有改动,加入下载
                            reloadFiles.Add(item.Key, item.Value);
                        }
                    }
                }
            }
            int maxCount = reloadFiles.Count;

            Debug.Log("下载资源数量:" + maxCount);
            int loadedCount = 0;

            foreach (var item in reloadFiles)
            {
                string url = Config.RemoteUrl + "/" + item.Key;
                Debug.Log("下载资源:" + url);
                UnityWebRequest resRequest = UnityWebRequest.Get(url);
                yield return(resRequest.SendWebRequest());

                if (resRequest.error != null)
                {
                    Debug.LogError(" [ " + url + " ] " + resRequest.error);
                    MessageCenter.Dispatch(MsgEnum.ABLoadingError, resRequest.error);
                }
                string savePath = LuaConst.localABPath + "/" + item.Key;
                string saveDir  = savePath.Substring(0, savePath.LastIndexOf("/"));
                if (!Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }
                File.WriteAllBytes(savePath, resRequest.downloadHandler.data);
                loadSuccessFiles.Add(item.Key, item.Value);
                loadedCount++;
                float progress = loadedCount / (float)maxCount;
                Debug.Log("下载进度:" + progress);
                MessageCenter.Dispatch(MsgEnum.ABLoadingProgress, progress);
            }
            UpdateLocalFiles();
            MessageCenter.Dispatch(MsgEnum.ABLoadingProgress, 1);
            yield return(new WaitForEndOfFrame());

            MessageCenter.Dispatch(MsgEnum.ABLoadingFinish);
            CommandController.Instance.Execute(CommandEnum.StartLuaMain);
        }