LaunchLoadMgr.cs 15 KB

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