LaunchLoadMgr.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class LaunchLoadMgr
  7. {
  8. private Action<bool> m_LaunchCompleteCb;
  9. private Action m_AssetMapInitCompleteCb;
  10. private bool m_IsCompleted = false;
  11. private float m_StartTime = 0f;
  12. private int m_TimerId = 0;
  13. public LaunchLoadMgr()
  14. {
  15. }
  16. ~LaunchLoadMgr()
  17. {
  18. StopLaunch();
  19. }
  20. public void StartLaunch(Action<bool> launchCompleteCb, Action assetMapInitCompleteCb)
  21. {
  22. if (m_IsCompleted)
  23. {
  24. StopLaunch();
  25. return;
  26. }
  27. m_LaunchCompleteCb = launchCompleteCb;
  28. m_AssetMapInitCompleteCb = assetMapInitCompleteCb;
  29. m_StartTime = Time.unscaledTime;
  30. m_IsCompleted = false;
  31. DownloadMgr.Instance.Init();
  32. LaunchThread.CreateInstance();
  33. AvatarRTMgr.CreateInstance();
  34. MusicMgr.Instance.InitMgr();
  35. UIMgr.Instance.InitMgr();
  36. //AssetsMgr.Instance.InitMgr();
  37. //AssetsMgr.Instance.InitDependenciesAsync(OnDependenciesInited);
  38. //GameObject go = ResourceMgr.Instance.LoadAssetSync<GameObject>(Constants.UIPath, "UILanuch/UILanuch");
  39. //OnLoadUILanuchCallback(go, null, null);
  40. AssetsMgr.Instance.InitAbNameCfg(()=>
  41. {
  42. AssetsMgr.Instance.InitDependenciesAsync(OnDependenciesInited);
  43. });
  44. }
  45. public void StopLaunch()
  46. {
  47. DisposeUILanuch();
  48. if (m_LaunchCompleteCb != null)
  49. {
  50. m_LaunchCompleteCb(m_IsCompleted);
  51. m_LaunchCompleteCb = null;
  52. }
  53. if (LaunchThread.HasInstance())
  54. {
  55. LaunchThread.DestroyInstance();
  56. }
  57. m_AssetMapInitCompleteCb = null;
  58. }
  59. private void OnDependenciesInited(bool success)
  60. {
  61. if (success)
  62. {
  63. if (m_AssetMapInitCompleteCb != null)
  64. m_AssetMapInitCompleteCb();
  65. AssetsMgr.Instance.InitResidentAssetBundleList(OnResidentAssetBundleListCompleted);
  66. }
  67. else
  68. DebugHelper.LogError("[wboy] Init Dependencies Fail ");
  69. }
  70. private void OnResidentAssetBundleListCompleted(bool success)
  71. {
  72. if (success)
  73. {
  74. ResourceMgr.Instance.LoadAsset<GameObject>(OnLoadUILanuchCallback, Constants.UIPath, "UILanuch/UILanuch");
  75. }
  76. else
  77. {
  78. DebugHelper.LogError("加载shader文件出错,请check!!!");
  79. }
  80. }
  81. private void OnLoadUILanuchCallback(GameObject sourceGo, string path_, string[] assetNames)
  82. {
  83. try
  84. {
  85. InitUILanuch(sourceGo);
  86. m_LoginAnimator.Play("LoginShow", -1);
  87. m_LoginAnimator.Update(0);
  88. m_LoginAnimator.enabled = false;
  89. float deltaTime = Time.unscaledTime - m_StartTime;
  90. int time = 500 - Mathf.CeilToInt(deltaTime * 1000);
  91. if (time <= 0) time = 1;
  92. m_TimerId = TimerManager.Instance.AddTimer(time, 1, StartUILanuchAnim);
  93. }
  94. catch (System.Exception e)
  95. {
  96. Debug.LogException(e);
  97. }
  98. }
  99. private IEnumerator CheckResUpdate()
  100. {
  101. DownloadMgr.Instance.CheckVersion();
  102. SetLoadDes("檢查更新");
  103. SetLoadPrecent(0);
  104. int pre = 0;
  105. yield return new WaitUntil(() =>
  106. {
  107. if (!DownloadMgr.Instance.CheckFinish)
  108. {
  109. pre += 10;
  110. if (pre >= 100)
  111. {
  112. pre = 98;
  113. }
  114. }
  115. else
  116. {
  117. pre = 100;
  118. }
  119. SetLoadPrecent(pre);
  120. return DownloadMgr.Instance.CheckFinish;
  121. });
  122. if (DownloadMgr.Instance.CheckNeedDownload())
  123. {
  124. SetLoadDes("資源更新");
  125. SetLoadPrecent(0);
  126. yield return new WaitUntil(() =>
  127. {
  128. bool isFinish = DownloadMgr.Instance.DownLoadFinish();
  129. if (isFinish)
  130. {
  131. //#if UNITY_IOS
  132. if (DownloadMgr.Instance.DowmloadError)
  133. {
  134. isFinish = false;
  135. SetLoadDes("資源更新失敗,請重新啟動遊戲下載資源");
  136. }
  137. else
  138. {
  139. SetLoadPrecent(100);
  140. }
  141. //#endif
  142. }
  143. else
  144. {
  145. float dsize = DownloadMgr.Instance.DownloadSize;
  146. float tsize = DownloadMgr.Instance.TotalSize;
  147. float press = (dsize / tsize) * 100;
  148. SetLoadPrecent((int)press);
  149. }
  150. return isFinish;
  151. });
  152. }
  153. yield return new WaitForSeconds(1);
  154. DownloadMgr.Instance.Free();
  155. GameMgr.Instance.CheckVersion();
  156. //m_LoadingRootGo.SetActive(false);
  157. //SetLoadEffectShowState(false);
  158. SetLoadDes("");
  159. SetLoadPrecent(0);
  160. FairGuard.Init();
  161. // 加载配置
  162. EventMgr.AddEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnConfigMgrInited);
  163. ConfigMgr.CreateInstance();
  164. }
  165. private void StartUILanuchAnim(int timerSequence)
  166. {
  167. TimerManager.Instance.RemoveTimer(timerSequence);
  168. m_TimerId = 0;
  169. m_LoginAnimator.enabled = true;
  170. m_LoginAnimator.Play("LoginShow", -1);
  171. m_LoginAnimator.Update(0);
  172. m_TimerId = TimerManager.Instance.AddTimer(2000, 1, OnLoadUILanuchAnimCompleted);
  173. }
  174. private void OnLoadUILanuchAnimCompleted(int timerSequence)
  175. {
  176. TimerManager.Instance.RemoveTimer(timerSequence);
  177. m_TimerId = 0;
  178. m_LoadingRootGo.SetActive(true);
  179. SetLoadEffectShowState(true);
  180. GameMgr.Instance.DoTaskByCorutine(CheckResUpdate);
  181. }
  182. private void OnConfigMgrInited(CoreEvent<int> ce)
  183. {
  184. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnConfigMgrInited);
  185. // m_LoadingRootGo.SetActive(true);
  186. //SetLoadEffectShowState(true);
  187. SetLoadDes("GameLogin17");
  188. SetLoadPrecent(10);
  189. if (m_AppVolLocalizeScript)
  190. m_AppVolLocalizeScript.OnChangeLang();
  191. if (m_ResVolLocalizeScript)
  192. m_ResVolLocalizeScript.OnChangeLang();
  193. int result = ce.Data;
  194. if (result == 1)
  195. {
  196. EventMgr.AddEventListener<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, OnLoadLuaOk);
  197. BattleFormulaParamMgr.CreateInstance();
  198. LuaMgr.Instance.InitMgr();
  199. }
  200. else
  201. {
  202. DebugHelper.LogError("加载配置文件出错,请check!!!");
  203. }
  204. }
  205. private void OnLoadLuaOk(CoreEvent<bool, float> ce)
  206. {
  207. bool result = ce.Data;
  208. int precent = Mathf.FloorToInt(Mathf.Clamp01(ce.Data1) * 80) + 10;
  209. SetLoadPrecent(precent);
  210. if (result)
  211. {
  212. EventMgr.RemoveEventListener<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, OnLoadLuaOk);
  213. SetLoadDes("GameLogin17");
  214. NetworkMgr.CreateInstance();
  215. ModelMgr.Instance.InitMgr();
  216. TowerBattleCfgMgr.CreateInstance();
  217. ResourceMgr.Instance.ReadResidentResCfg();
  218. EventMgr.AddEventListener<int>(ECoreEventType.EID_LOADKEY_OK, OnLoadKeyOk);
  219. SecurityLayer.Instance.Init();
  220. }
  221. }
  222. private void OnLoadKeyOk(CoreEvent<int> ce)
  223. {
  224. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LOADKEY_OK, OnLoadKeyOk);
  225. SetLoadDes("GameLogin17");
  226. SetLoadPrecent(90);
  227. int result = ce.Data;
  228. if (result == 1)
  229. {
  230. EventMgr.AddEventListener<int>(ECoreEventType.EID_CameraMgrInited, OnCameraMgrInited);
  231. CameraMgr.Instance.InitMgr();
  232. }
  233. else
  234. {
  235. DebugHelper.LogError("加载Security文件出错,请check!!!");
  236. }
  237. }
  238. private void OnCameraMgrInited(CoreEvent<int> ce)
  239. {
  240. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_CameraMgrInited, OnCameraMgrInited);
  241. SetLoadDes("GameLogin17");
  242. SetLoadPrecent(95);
  243. int result = ce.Data;
  244. if (result == 1)
  245. {
  246. ResourceMgr.Instance.LoadAsset<GameObject>(OnLoadClickEffectCallback, Constants.UIEffectPath, "FX_UI_DianJi");
  247. }
  248. else
  249. {
  250. DebugHelper.LogError("Camera 初始化失败,请check!!!");
  251. }
  252. }
  253. private void OnLoadClickEffectCallback(GameObject sourceGo, string path_, string[] assetNames_)
  254. {
  255. SetLoadDes("GameLogin17");
  256. SetLoadPrecent(99);
  257. GameObject go = GameObject.Instantiate(sourceGo);
  258. Transform trans = go.transform;
  259. trans.SetParent(UIMgr.Instance.ClickEffectTrans);
  260. trans.localPosition = Vector3.zero;
  261. trans.localRotation = Quaternion.identity;
  262. trans.localScale = Vector3.one;
  263. ResourceMgr.Instance.LoadDirAsset<List<GameObject>>(OnLoadUICommonPrefabCallback, Constants.UICommonPath);
  264. }
  265. private void OnLoadUICommonPrefabCallback(List<GameObject> objs, string path_, string[] assetNames_)
  266. {
  267. SetLoadDes("GameLogin17");
  268. SetLoadPrecent(100);
  269. m_IsCompleted = true;
  270. StopLaunch();
  271. }
  272. private GameObject m_LanuchGo;
  273. private GameObject m_LogoGo;
  274. private GameObject m_LoginRootGo;
  275. private Animator m_LoginAnimator;
  276. private GameObject m_LoadingRootGo;
  277. private Text m_LoadDesTxt;
  278. private Text m_LoadPrecentTxt;
  279. private Scrollbar m_LoadScrollBar;
  280. private GameObject m_LoadEffectGo;
  281. private TMPro.TextMeshProUGUI m_AppVolTxt;
  282. private UILocalizeScript m_AppVolLocalizeScript;
  283. private TMPro.TextMeshProUGUI m_ResVolTxt;
  284. private UILocalizeScript m_ResVolLocalizeScript;
  285. private GameObject m_EditionGo;
  286. private void InitUILanuch(GameObject sourceGo)
  287. {
  288. // 这里初始化保持和UIBase一致,方便后续对象池回收并使用
  289. GameObject pageBody = new GameObject("UILanuch");
  290. Canvas canvas = pageBody.AddComponent<Canvas>();
  291. pageBody.AddComponent<GraphicRaycaster>();
  292. CanvasGroup canvasGroup = pageBody.AddComponent<CanvasGroup>();
  293. if (canvasGroup != null)
  294. canvasGroup.interactable = false;
  295. pageBody.layer = LayerMask.NameToLayer("UI");
  296. var pageBodyTrans = pageBody.transform;
  297. pageBodyTrans.SetParent(UIMgr.Instance.UIRootTrans);
  298. canvas.overrideSorting = true;
  299. RectTransform rectTrans = pageBody.GetComponent<RectTransform>();
  300. if (rectTrans != null)
  301. {
  302. rectTrans.localPosition = Vector3.zero;
  303. rectTrans.localScale = Vector3.one;
  304. rectTrans.anchorMin = new Vector2(0, 0);
  305. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  306. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  307. rectTrans.anchoredPosition = Vector2.zero;
  308. rectTrans.sizeDelta = Vector2.zero;
  309. }
  310. GameObject rootRage = GameObject.Instantiate(sourceGo);
  311. rootRage.name = "Root";
  312. rootRage.SetActive(true);
  313. CommonUtil.SetGameObjectLayer(rootRage, "UI");
  314. rectTrans = rootRage.GetComponent<RectTransform>();
  315. rectTrans.SetParent(pageBodyTrans, false);
  316. rectTrans.localScale = Vector3.one;
  317. rectTrans.anchorMin = new Vector2(0, 0);
  318. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  319. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  320. rectTrans.anchoredPosition3D = Vector3.zero;
  321. rectTrans.sizeDelta = Vector2.zero;
  322. m_LanuchGo = pageBody;
  323. Transform rootTrans = rectTrans.Find("LoginAnim");
  324. m_LoginAnimator = rootTrans.GetComponent<Animator>();
  325. m_LogoGo = rootTrans.Find("Logo").gameObject;
  326. m_LoginRootGo = rootTrans.Find("LoginRoot").gameObject;
  327. Transform loadingRootTrans = rootTrans.Find("LoadingRoot");
  328. m_LoadingRootGo = loadingRootTrans.gameObject;
  329. Transform LoadingBarTrans = loadingRootTrans.Find("LoadingBar");
  330. m_LoadDesTxt = LoadingBarTrans.Find("Text").GetComponent<Text>();
  331. m_LoadPrecentTxt = LoadingBarTrans.Find("NumberText").GetComponent<Text>();
  332. Transform scrollBarTrans = LoadingBarTrans.Find("Scrollbar");
  333. m_LoadScrollBar = scrollBarTrans.GetComponent<Scrollbar>();
  334. m_LoadEffectGo = scrollBarTrans.Find("Sliding Area/Handle/Particle").gameObject;
  335. Transform volTrans = rootTrans.Find("Common/AppVol");
  336. m_AppVolTxt = volTrans.Find("ContentTxt").GetComponent<TMPro.TextMeshProUGUI>();
  337. m_AppVolLocalizeScript = volTrans.Find("NameTxt").GetComponent<UILocalizeScript>();
  338. volTrans = rootTrans.Find("Common/ResVol");
  339. m_ResVolTxt = volTrans.Find("ContentTxt").GetComponent<TMPro.TextMeshProUGUI>();
  340. m_ResVolLocalizeScript = volTrans.Find("NameTxt").GetComponent<UILocalizeScript>();
  341. m_EditionGo = rootTrans.Find("Common/EditionTxt").gameObject;
  342. m_AppVolTxt.text = GameMgr.Instance.GameVersion;
  343. m_ResVolTxt.text = GameMgr.Instance.ResVersion;
  344. m_LogoGo.SetActive(true);
  345. m_LoginRootGo.SetActive(false);
  346. m_LoadingRootGo.SetActive(false);
  347. #if HEALTH_BULLETIN
  348. m_EditionGo.SetActive(true);
  349. #else
  350. m_EditionGo.SetActive(false);
  351. #endif
  352. SetLoadEffectShowState(false);
  353. SetLoadDes("");
  354. SetLoadPrecent(0);
  355. }
  356. private void DisposeUILanuch()
  357. {
  358. if (m_LanuchGo)
  359. {
  360. ResourceMgr.Instance.RecycleUIGO(Constants.UIPath, "UILanuch/UILanuch_body", m_LanuchGo);
  361. m_LanuchGo = null;
  362. }
  363. m_LogoGo = null;
  364. m_LoginRootGo = null;
  365. m_LoadingRootGo = null;
  366. m_EditionGo = null;
  367. m_LoadDesTxt = null;
  368. m_LoadPrecentTxt = null;
  369. m_LoadScrollBar = null;
  370. m_LoadEffectGo = null;
  371. m_AppVolTxt = null;
  372. m_AppVolLocalizeScript = null;
  373. m_ResVolTxt = null;
  374. m_ResVolLocalizeScript = null;
  375. }
  376. private void SetLoadEffectShowState(bool state)
  377. {
  378. m_LoadEffectGo.SetActive(state);
  379. }
  380. private void SetLoadDes(string langKey)
  381. {
  382. if (m_LoadDesTxt)
  383. {
  384. if (string.IsNullOrEmpty(langKey))
  385. m_LoadDesTxt.text = "";
  386. else
  387. m_LoadDesTxt.text = I18N.T(langKey);
  388. }
  389. }
  390. private void SetLoadPrecent(int precent)
  391. {
  392. precent = Mathf.Clamp(precent, 0, 100);
  393. if (m_LoadPrecentTxt)
  394. m_LoadPrecentTxt.text = precent + "%";
  395. if (m_LoadScrollBar)
  396. m_LoadScrollBar.size = precent * 0.01f;
  397. }
  398. }