Launcher.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System;
  6. using Newtonsoft.Json;
  7. using System.Text;
  8. using System.IO;
  9. using UnityEngine.Networking;
  10. using Newtonsoft.Json.Linq;
  11. using YLSJ;
  12. /// <summary>
  13. /// 游戏启动器
  14. /// </summary>
  15. public class Launcher : MonoBehaviour
  16. {
  17. /// <summary>
  18. /// Panel
  19. /// </summary>
  20. private GameObject mPanel = null;
  21. /// <summary>
  22. /// 显示百分比的文本框
  23. /// </summary>
  24. private Text mProgressText = null;
  25. /// <summary>
  26. /// 更新进度条
  27. /// </summary>
  28. private Slider mUpdateSlider = null;
  29. /// <summary>
  30. /// 显示提示的
  31. /// </summary>
  32. private Text mTipsText = null;
  33. /// <summary>
  34. /// 更新标题
  35. /// </summary>
  36. private Text mUpdateTitleText = null;
  37. /// <summary>
  38. /// 是否开始更新
  39. /// </summary>
  40. private bool startUpdate = false;
  41. /// <summary>
  42. /// 更新信息
  43. /// </summary>
  44. private UpdateInfo mUpdateInfo = null;
  45. // 结果
  46. private CheckUpdate.CheckResult mCheckResult = null;
  47. private string apkURL = "";
  48. /// <summary>
  49. /// 开始
  50. /// </summary>
  51. private void Start()
  52. {
  53. // 友盟统计: 开始更新之前
  54. AndroidInteractive.CallJavaFunction("onEvent", "e_k_9", "1", "1");
  55. StatHelper.Instance.AddEvent( "e_k_9", "1", "1");
  56. Init();
  57. string plat = "";
  58. string channel = "TapTap";
  59. if (Application.platform == RuntimePlatform.IPhonePlayer)
  60. {
  61. plat = "IOS";
  62. }
  63. else
  64. {
  65. plat = "Android";
  66. }
  67. // 检测更新
  68. UpdateHelper.Instance().GetCurVersion((ver) =>
  69. {
  70. StartCoroutine(IE_GetUpdate(ver, plat, channel));
  71. });
  72. }
  73. /// <summary>
  74. /// 获取更新
  75. /// </summary>
  76. /// <param name="myVer"></param>
  77. /// <param name="plat"></param>
  78. /// <returns></returns>
  79. private IEnumerator IE_GetUpdate(int myVer, string plat, string channel)
  80. {
  81. string url = CheckUpdate.Check_URL;
  82. UnityWebRequest www = UnityWebRequest.Get(url + "?clientVer=" + myVer + "&plat=" + plat + "&channel=" + channel);
  83. yield return www.SendWebRequest();
  84. while (!www.isDone)
  85. {
  86. yield return new WaitForSeconds(0.02f);
  87. }
  88. try
  89. {
  90. JObject jObj = JObject.Parse(www.downloadHandler.text);
  91. mCheckResult = jObj.ToObject<CheckUpdate.CheckResult>();
  92. int a = 1;
  93. }
  94. catch
  95. {
  96. mCheckResult = null;
  97. UI_CueDialog.Instance().Open("获取服务器地址失败!", "更新提示", E_DialogType.OneButton, ExitGame);
  98. }
  99. if (mCheckResult != null)
  100. {
  101. // 无更新
  102. if (mCheckResult.err == 0)
  103. {
  104. mPanel.SetActive(false);
  105. LoadAssembly();
  106. }
  107. // 整包更新
  108. else if (mCheckResult.err == 1031)
  109. {
  110. apkURL = mCheckResult.fullDownloadPage;
  111. UI_CueDialog.Instance().Open(mCheckResult.msg, "更新提示", E_DialogType.OneButton, UpdateAPK);
  112. }
  113. // 有更新
  114. else if (mCheckResult.err == 1032)
  115. {
  116. UI_CueDialog.Instance().Open(mCheckResult.msg, "更新提示", E_DialogType.TwoBntton, CueHaveUpdate, ExitGame);
  117. }
  118. // 通知,不可继续游戏
  119. else if (mCheckResult.err == 1033)
  120. {
  121. UI_CueDialog.Instance().Open(mCheckResult.msg, "更新提示", E_DialogType.OneButton, ExitGame);
  122. }
  123. }
  124. www.Dispose();
  125. }
  126. /// <summary>
  127. /// 弹窗提示回调
  128. /// </summary>
  129. private void CueHaveUpdate()
  130. {
  131. // 获取更新文件
  132. UpdateHelper.Instance().GetUpdateFile(mCheckResult.cdn,()=>
  133. {
  134. mUpdateInfo = UpdateHelper.Instance().GetUpdateInfo();
  135. startUpdate = true;
  136. mPanel.gameObject.SetActive(true);
  137. });
  138. }
  139. /// <summary>
  140. /// 加载字符集
  141. /// </summary>
  142. private void LoadAssembly()
  143. {
  144. mPanel.SetActive(false);
  145. mUpdateTitleText.text = "正在初始化中......";
  146. // 开启更新检测
  147. if (GlobalConfig.is_Update)
  148. {
  149. AssemblyHelper.Instance.InitializeAssembly(Constant.AssemblyFilePath, Constant.AssemblyName, () =>
  150. {
  151. AssemblyHelper.Instance.BindScript("Entrance_Launcher", this.gameObject);
  152. });
  153. }
  154. else
  155. {
  156. mPanel.SetActive(false);
  157. AssemblyHelper.Instance.BindScript("Entrance_Launcher", this.gameObject);
  158. }
  159. }
  160. /// <summary>
  161. /// 初始化
  162. /// </summary>
  163. public void Init()
  164. {
  165. GameObject obj = GameObject.Find("UI_ONUpdateWindow");
  166. mPanel = obj.transform.Find("Panel").gameObject;
  167. mUpdateSlider = obj.transform.Find("Panel/UpdatePanel/Slider").GetComponent<Slider>();
  168. mProgressText = obj.transform.Find("Panel/UpdatePanel/percentText").GetComponent<Text>();
  169. mUpdateTitleText = obj.transform.Find("Panel/UpdatePanel/UpdateFileText").GetComponent<Text>();
  170. mUpdateTitleText.text = "正在检查更新......";
  171. mTipsText = obj.transform.Find("Panel/UpdatePanel/TipsText").GetComponent<Text>();
  172. }
  173. /// <summary>
  174. /// 更新
  175. /// </summary>
  176. private void Update()
  177. {
  178. if (startUpdate == false)
  179. {
  180. return;
  181. }
  182. if (mUpdateInfo != null)
  183. {
  184. // 更新完成,切换到登陆关卡
  185. if (mUpdateInfo.mUpdateComplete)
  186. {
  187. mUpdateSlider.value = 1;
  188. mUpdateInfo = null;
  189. mUpdateTitleText.text = "更新完成";
  190. mTipsText.text = "开始初始化游戏,此过程不消耗流量......";
  191. mProgressText.text = "100%";
  192. startUpdate = false;
  193. LoadAssembly();
  194. AndroidInteractive.CallJavaFunction("onEvent", "e_k_1", "1", "1"); // 资源更新完成
  195. StatHelper.Instance.AddEvent( "e_k_1", "complete", "1"); // 资源更新完成
  196. }
  197. else
  198. {
  199. if (mUpdateInfo.mTotalFileCount > 0)
  200. {
  201. mUpdateTitleText.text = "正在更新中......";
  202. mTipsText.text = "更新进度: " + (mUpdateInfo.mCurFileIndex + 1) + "/" + mUpdateInfo.mTotalFileCount;
  203. mUpdateSlider.value = mUpdateInfo.mCurFileProgress;
  204. mProgressText.text = (int)(mUpdateInfo.mCurFileProgress * 100) + "%";
  205. }
  206. }
  207. }
  208. }
  209. /// <summary>
  210. /// 整包更新
  211. /// </summary>
  212. private void UpdateAPK()
  213. {
  214. Application.OpenURL(apkURL);
  215. Application.Quit();
  216. }
  217. /// <summary>
  218. /// 退出游戏
  219. /// </summary>
  220. private void ExitGame()
  221. {
  222. Application.Quit();
  223. }
  224. }