public void PostPerFrameTask(VoidFuncVoid task) { if (perFrameTasks == null) { return; } this.perFrameTasks.Enqueue(task); }
//post event to main thread public void Post(VoidFuncVoid task) { if (task == null) { return; } lock (this.tasks) { this.tasks.Enqueue(task); } }
public void TryDownloadOnly(string uuid, VoidFuncVoid cb_ok, VoidFuncVoid cb_error) { if (string.IsNullOrEmpty(uuid)) { return; } if (this._running_task.Contains(uuid)) { #if UNITY_EDITOR Debug.LogWarning("MapHttpTask this map has downloading " + uuid); #endif //has running return; } StartCoroutine(_CoTryDownloadOnly(uuid, cb_ok, cb_error)); }
IEnumerator _CoTryDownloadOnly(string uuid, VoidFuncVoid cb_ok, VoidFuncVoid cb_error) { string name = uuid; //load from local file cache bool save_file_ok = true; var time = Utils.GetTimestampMiliseconds(); string json = Serializable.LoadFromFileEx(name + ".json"); if (string.IsNullOrEmpty(json)) { #if UNITY_EDITOR Debug.Log(" missing map file cache start download with www " + name); #endif if (this._running_task.Contains(uuid)) { } else { this._running_task.Add(uuid); } int try_times = 3; while (try_times > 0) { var www = new WWW(Serializable.GetDownloadWWWUrl(name)); yield return(www); #if UNITY_EDITOR Debug.Log(www.url + " " + www.text); #else // Debug.Log(www.url); #endif if (Serializable.Map.IsMapJson(www.text)) { save_file_ok = Serializable.SaveToFile(name + ".json", www.text); json = www.text;; break; } else { if (www.text.IndexOf("Document not found") > 0 && www.text.Length < 150) { Debug.LogError("MapHttpTask Fatal error:This Map HasNot exist in web server " + name + " url=" + www.url); try_times = -1; break; } //error try_times--; continue; } } if (this._running_task.Contains(uuid)) { this._running_task.Remove(uuid); } } if (Serializable.Map.IsMapJson(json) == false || !save_file_ok) { BattleServer.TryDisconnected(); if (cb_error != null) { cb_error(); } } else { if (cb_ok != null) { cb_ok(); } } time = (Utils.GetTimestampMiliseconds() - time); #if UNITY_EDITOR if (time > 2000) { Debug.LogError(" MapHttpTask.AddDownloadTask load " + name + ".json time =" + time + " ms"); } else { Debug.Log(" MapHttpTask.AddDownloadTask load " + name + ".json time =" + time + " ms"); } #endif }
//for map editor //async download or load in local file cache public static IEnumerator Download(string name, VoidFuncString ok, VoidFuncVoid error = null, bool ignore_error = false) { //load from local file cache var time = Utils.GetTimestampMiliseconds(); string json = Serializable.LoadFromFileEx(name + ".json"); bool save_file_ok = true; if (string.IsNullOrEmpty(json)) { #if UNITY_EDITOR Debug.Log(" missing map file cache start download with www " + name); #endif int try_times = 3; while (try_times > 0) { string url = Serializable.GetDownloadWWWUrl(name); var www = new WWW(url); yield return(www); #if UNITY_EDITOR Debug.Log(url + " " + www.text); #else Debug.Log(url); #endif if (Serializable.Map.IsMapJson(www.text)) { save_file_ok = Serializable.SaveToFile(name + ".json", www.text); //ok json = www.text; break; } else { if (www.text.IndexOf("Document not found") > 0 && www.text.Length < 150) { Debug.LogError("MapHttpTask Fatal error:This Map HasNot exist in web server " + name + " url=" + url); try_times = -1; break; } //error try_times--; continue; } } } if (Serializable.Map.IsMapJson(json) == false || !save_file_ok) { BattleServer.TryDisconnected(); if (!ignore_error) { if (UICommonDialog.ins != null) { UICommonDialog.ins.ShowOKCancle("地图下载失败 [error " + name + "] ", () => { SceneMgr.LoadLevel("GameLogin"); }, () => { SceneMgr.LoadLevel("GameLogin"); }); } } if (error != null) { error(); } } else { if (ok != null) { ok(json); } } time = (Utils.GetTimestampMiliseconds() - time); #if UNITY_EDITOR if (time > 2000) { Debug.LogError(" MapHttpTask.Download load " + name + ".json time =" + time + " ms"); } else { Debug.Log(" MapHttpTask.Download load " + name + ".json time =" + time + " ms"); } #endif }