LaunchLoadMgr.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 void CheckISShenHe()
  101. {
  102. DownloadMgr.Instance.InitUrlCfg();
  103. DownLoadUrlCfg cfg = DownloadMgr.Instance.GetCheckSHCfg();
  104. bool isOpenCheck = false;
  105. string url = "";
  106. string param = "";
  107. bool active = true;
  108. bool lpactive = true;
  109. if (cfg!=null)
  110. {
  111. isOpenCheck = FileHelper.CheckStringIsTrue(cfg.BaseUrl);
  112. url = cfg.ServerUrl;
  113. param = cfg.Param1;
  114. active = FileHelper.CheckStringIsTrue(cfg.DownloadPath);
  115. lpactive = cfg.MaxNum == 1;
  116. }
  117. if (isOpenCheck)
  118. {
  119. string checkurl = $"{url}{param}";
  120. AssetDownloader.Instance.DownLoadFileByCoroutine(checkurl, webreq =>
  121. {
  122. bool isShenHe = true;
  123. if (webreq != null)
  124. {
  125. string data = webreq.downloadHandler.text;
  126. isShenHe = data == "1" || data == "\"1\"";
  127. }
  128. if (isShenHe)
  129. {
  130. SetLoadScrollBarActive(active);
  131. SetLoadPrecentActive(lpactive);
  132. OnEndDownload();
  133. }
  134. else
  135. {
  136. GameMgr.Instance.DoTaskByCorutine(CheckResUpdate);
  137. }
  138. },2);
  139. }
  140. else
  141. {
  142. GameMgr.Instance.DoTaskByCorutine(CheckResUpdate);
  143. }
  144. }
  145. private IEnumerator CheckResUpdate()
  146. {
  147. m_LoadingRootGo.SetActive(true);
  148. DownloadMgr.Instance.CheckVersion();
  149. SetLoadDes("CheckRes");
  150. SetLoadPrecent(0);
  151. int pre = 0;
  152. yield return new WaitUntil(() =>
  153. {
  154. if (!DownloadMgr.Instance.CheckFinish)
  155. {
  156. pre += 10;
  157. if (pre >= 100)
  158. {
  159. pre = 98;
  160. }
  161. }
  162. else
  163. {
  164. pre = 100;
  165. }
  166. SetLoadPrecent(pre);
  167. return DownloadMgr.Instance.CheckFinish;
  168. });
  169. if (DownloadMgr.Instance.CheckNeedDownload())
  170. {
  171. SetLoadDes("DownloadRes");
  172. SetLoadPrecent(0);
  173. yield return new WaitUntil(() =>
  174. {
  175. bool isFinish = DownloadMgr.Instance.DownLoadFinish();
  176. if (isFinish)
  177. {
  178. //#if UNITY_IOS
  179. if (DownloadMgr.Instance.DowmloadError)
  180. {
  181. isFinish = false;
  182. SetLoadDes("GameLogin20");
  183. }
  184. else
  185. {
  186. SetLoadPrecent(100);
  187. }
  188. //#endif
  189. }
  190. else
  191. {
  192. float dsize = DownloadMgr.Instance.DownloadSize;
  193. float tsize = DownloadMgr.Instance.TotalSize;
  194. float press = (dsize / tsize) * 100;
  195. SetLoadPrecent((int)press);
  196. }
  197. return isFinish;
  198. });
  199. }
  200. yield return new WaitForSeconds(1);
  201. DownloadMgr.Instance.Free();
  202. OnEndDownload();
  203. }
  204. private void OnEndDownload()
  205. {
  206. m_LoadingRootGo.SetActive(true);
  207. GameMgr.Instance.CheckVersion();
  208. GameMgr.Instance.SetSDKName(DownloadMgr.Instance.GetSDKName());
  209. //m_LoadingRootGo.SetActive(false);
  210. //SetLoadEffectShowState(false);
  211. SetLoadDes("");
  212. SetLoadPrecent(0);
  213. FairGuard.Init();
  214. // 加载配置
  215. EventMgr.AddEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnConfigMgrInited);
  216. ConfigMgr.Instance.Init();
  217. }
  218. private void StartUILanuchAnim(int timerSequence)
  219. {
  220. TimerManager.Instance.RemoveTimer(timerSequence);
  221. m_TimerId = 0;
  222. m_LoginAnimator.enabled = true;
  223. m_LoginAnimator.Play("LoginShow", -1);
  224. m_LoginAnimator.Update(0);
  225. m_TimerId = TimerManager.Instance.AddTimer(2000, 1, OnLoadUILanuchAnimCompleted);
  226. }
  227. private void OnLoadUILanuchAnimCompleted(int timerSequence)
  228. {
  229. TimerManager.Instance.RemoveTimer(timerSequence);
  230. m_TimerId = 0;
  231. //m_LoadingRootGo.SetActive(true);
  232. SetLoadEffectShowState(true);
  233. //GameMgr.Instance.DoTaskByCorutine(CheckResUpdate);
  234. // 加载配置
  235. EventMgr.AddEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnLoadCfgEnd);
  236. ConfigMgr.CreateInstance();
  237. }
  238. private void OnLoadCfgEnd(CoreEvent<int> ce)
  239. {
  240. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnLoadCfgEnd);
  241. //GameMgr.Instance.DoTaskByCorutine(CheckResUpdate);
  242. CheckISShenHe();
  243. }
  244. private void OnConfigMgrInited(CoreEvent<int> ce)
  245. {
  246. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnConfigMgrInited);
  247. // m_LoadingRootGo.SetActive(true);
  248. //SetLoadEffectShowState(true);
  249. SetLoadDes("GameLogin17");
  250. SetLoadPrecent(10);
  251. if (m_AppVolLocalizeScript)
  252. m_AppVolLocalizeScript.OnChangeLang();
  253. if (m_ResVolLocalizeScript)
  254. m_ResVolLocalizeScript.OnChangeLang();
  255. int result = ce.Data;
  256. if (result == 1)
  257. {
  258. EventMgr.AddEventListener<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, OnLoadLuaOk);
  259. BattleFormulaParamMgr.CreateInstance();
  260. LuaMgr.Instance.InitMgr();
  261. }
  262. else
  263. {
  264. DebugHelper.LogError("加载配置文件出错,请check!!!");
  265. }
  266. }
  267. private void OnLoadLuaOk(CoreEvent<bool, float> ce)
  268. {
  269. bool result = ce.Data;
  270. int precent = Mathf.FloorToInt(Mathf.Clamp01(ce.Data1) * 80) + 10;
  271. SetLoadPrecent(precent);
  272. if (result)
  273. {
  274. EventMgr.RemoveEventListener<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, OnLoadLuaOk);
  275. SetLoadDes("GameLogin17");
  276. NetworkMgr.CreateInstance();
  277. ModelMgr.Instance.InitMgr();
  278. TowerBattleCfgMgr.CreateInstance();
  279. ResourceMgr.Instance.ReadResidentResCfg();
  280. EventMgr.AddEventListener<int>(ECoreEventType.EID_LOADKEY_OK, OnLoadKeyOk);
  281. SecurityLayer.Instance.Init();
  282. }
  283. }
  284. private void OnLoadKeyOk(CoreEvent<int> ce)
  285. {
  286. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LOADKEY_OK, OnLoadKeyOk);
  287. SetLoadDes("GameLogin17");
  288. SetLoadPrecent(90);
  289. int result = ce.Data;
  290. if (result == 1)
  291. {
  292. EventMgr.AddEventListener<int>(ECoreEventType.EID_CameraMgrInited, OnCameraMgrInited);
  293. CameraMgr.Instance.InitMgr();
  294. }
  295. else
  296. {
  297. DebugHelper.LogError("加载Security文件出错,请check!!!");
  298. }
  299. }
  300. private void OnCameraMgrInited(CoreEvent<int> ce)
  301. {
  302. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_CameraMgrInited, OnCameraMgrInited);
  303. SetLoadDes("GameLogin17");
  304. SetLoadPrecent(95);
  305. int result = ce.Data;
  306. if (result == 1)
  307. {
  308. ResourceMgr.Instance.LoadAsset<GameObject>(OnLoadClickEffectCallback, Constants.UIEffectPath, "FX_UI_DianJi");
  309. }
  310. else
  311. {
  312. DebugHelper.LogError("Camera 初始化失败,请check!!!");
  313. }
  314. }
  315. private void OnLoadClickEffectCallback(GameObject sourceGo, string path_, string[] assetNames_)
  316. {
  317. SetLoadDes("GameLogin17");
  318. SetLoadPrecent(99);
  319. GameObject go = GameObject.Instantiate(sourceGo);
  320. Transform trans = go.transform;
  321. trans.SetParent(UIMgr.Instance.ClickEffectTrans);
  322. trans.localPosition = Vector3.zero;
  323. trans.localRotation = Quaternion.identity;
  324. trans.localScale = Vector3.one;
  325. ResourceMgr.Instance.LoadDirAsset<List<GameObject>>(OnLoadUICommonPrefabCallback, Constants.UICommonPath);
  326. }
  327. private void OnLoadUICommonPrefabCallback(List<GameObject> objs, string path_, string[] assetNames_)
  328. {
  329. SetLoadDes("GameLogin17");
  330. SetLoadPrecent(100);
  331. m_IsCompleted = true;
  332. StopLaunch();
  333. }
  334. private GameObject m_LanuchGo;
  335. private GameObject m_LogoGo;
  336. private GameObject m_LoginRootGo;
  337. private Animator m_LoginAnimator;
  338. private GameObject m_LoadingRootGo;
  339. private Text m_LoadDesTxt;
  340. private Text m_LoadPrecentTxt;
  341. private Scrollbar m_LoadScrollBar;
  342. private GameObject m_LoadEffectGo;
  343. private TMPro.TextMeshProUGUI m_AppVolTxt;
  344. private UILocalizeScript m_AppVolLocalizeScript;
  345. private TMPro.TextMeshProUGUI m_ResVolTxt;
  346. private UILocalizeScript m_ResVolLocalizeScript;
  347. private GameObject m_EditionGo;
  348. private void InitUILanuch(GameObject sourceGo)
  349. {
  350. // 这里初始化保持和UIBase一致,方便后续对象池回收并使用
  351. GameObject pageBody = new GameObject("UILanuch");
  352. Canvas canvas = pageBody.AddComponent<Canvas>();
  353. pageBody.AddComponent<GraphicRaycaster>();
  354. CanvasGroup canvasGroup = pageBody.AddComponent<CanvasGroup>();
  355. if (canvasGroup != null)
  356. canvasGroup.interactable = false;
  357. pageBody.layer = LayerMask.NameToLayer("UI");
  358. var pageBodyTrans = pageBody.transform;
  359. pageBodyTrans.SetParent(UIMgr.Instance.UIRootTrans);
  360. canvas.overrideSorting = true;
  361. RectTransform rectTrans = pageBody.GetComponent<RectTransform>();
  362. if (rectTrans != null)
  363. {
  364. rectTrans.localPosition = Vector3.zero;
  365. rectTrans.localScale = Vector3.one;
  366. rectTrans.anchorMin = new Vector2(0, 0);
  367. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  368. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  369. rectTrans.anchoredPosition = Vector2.zero;
  370. rectTrans.sizeDelta = Vector2.zero;
  371. }
  372. GameObject rootRage = GameObject.Instantiate(sourceGo);
  373. rootRage.name = "Root";
  374. rootRage.SetActive(true);
  375. CommonUtil.SetGameObjectLayer(rootRage, "UI");
  376. rectTrans = rootRage.GetComponent<RectTransform>();
  377. rectTrans.SetParent(pageBodyTrans, false);
  378. rectTrans.localScale = Vector3.one;
  379. rectTrans.anchorMin = new Vector2(0, 0);
  380. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  381. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  382. rectTrans.anchoredPosition3D = Vector3.zero;
  383. rectTrans.sizeDelta = Vector2.zero;
  384. m_LanuchGo = pageBody;
  385. Transform rootTrans = rectTrans.Find("LoginAnim");
  386. m_LoginAnimator = rootTrans.GetComponent<Animator>();
  387. m_LogoGo = rootTrans.Find("Logo").gameObject;
  388. m_LoginRootGo = rootTrans.Find("LoginRoot").gameObject;
  389. Transform loadingRootTrans = rootTrans.Find("LoadingRoot");
  390. m_LoadingRootGo = loadingRootTrans.gameObject;
  391. Transform LoadingBarTrans = loadingRootTrans.Find("LoadingBar");
  392. m_LoadDesTxt = LoadingBarTrans.Find("Text").GetComponent<Text>();
  393. m_LoadPrecentTxt = LoadingBarTrans.Find("NumberText").GetComponent<Text>();
  394. Transform scrollBarTrans = LoadingBarTrans.Find("Scrollbar");
  395. m_LoadScrollBar = scrollBarTrans.GetComponent<Scrollbar>();
  396. m_LoadEffectGo = scrollBarTrans.Find("Sliding Area/Handle/Particle").gameObject;
  397. Transform volTrans = rootTrans.Find("Common/AppVol");
  398. m_AppVolTxt = volTrans.Find("ContentTxt").GetComponent<TMPro.TextMeshProUGUI>();
  399. m_AppVolLocalizeScript = volTrans.Find("NameTxt").GetComponent<UILocalizeScript>();
  400. volTrans = rootTrans.Find("Common/ResVol");
  401. m_ResVolTxt = volTrans.Find("ContentTxt").GetComponent<TMPro.TextMeshProUGUI>();
  402. m_ResVolLocalizeScript = volTrans.Find("NameTxt").GetComponent<UILocalizeScript>();
  403. m_EditionGo = rootTrans.Find("Common/EditionTxt").gameObject;
  404. m_AppVolTxt.text = GameMgr.Instance.GameVersion;
  405. m_ResVolTxt.text = GameMgr.Instance.ResVersion;
  406. m_LogoGo.SetActive(true);
  407. m_LoginRootGo.SetActive(false);
  408. m_LoadingRootGo.SetActive(false);
  409. #if HEALTH_BULLETIN
  410. m_EditionGo.SetActive(true);
  411. #else
  412. m_EditionGo.SetActive(false);
  413. #endif
  414. SetLoadEffectShowState(false);
  415. SetLoadDes("");
  416. SetLoadPrecent(0);
  417. }
  418. private void DisposeUILanuch()
  419. {
  420. if (m_LanuchGo)
  421. {
  422. ResourceMgr.Instance.RecycleUIGO(Constants.UIPath, "UILanuch/UILanuch_body", m_LanuchGo);
  423. m_LanuchGo = null;
  424. }
  425. m_LogoGo = null;
  426. m_LoginRootGo = null;
  427. m_LoadingRootGo = null;
  428. m_EditionGo = null;
  429. m_LoadDesTxt = null;
  430. m_LoadPrecentTxt = null;
  431. m_LoadScrollBar = null;
  432. m_LoadEffectGo = null;
  433. m_AppVolTxt = null;
  434. m_AppVolLocalizeScript = null;
  435. m_ResVolTxt = null;
  436. m_ResVolLocalizeScript = null;
  437. }
  438. private void SetLoadEffectShowState(bool state)
  439. {
  440. m_LoadEffectGo.SetActive(state);
  441. }
  442. private void SetLoadDes(string langKey)
  443. {
  444. if (m_LoadDesTxt)
  445. {
  446. if (string.IsNullOrEmpty(langKey))
  447. m_LoadDesTxt.text = "";
  448. else
  449. m_LoadDesTxt.text = I18N.T(langKey);
  450. }
  451. }
  452. private void SetLoadPrecent(int precent)
  453. {
  454. precent = Mathf.Clamp(precent, 0, 100);
  455. if (m_LoadPrecentTxt)
  456. m_LoadPrecentTxt.text = precent + "%";
  457. if (m_LoadScrollBar)
  458. m_LoadScrollBar.size = precent * 0.01f;
  459. }
  460. private void SetLoadScrollBarActive(bool active)
  461. {
  462. m_LoadScrollBar.gameObject.SetActive(active);
  463. }
  464. private void SetLoadPrecentActive(bool active)
  465. {
  466. m_LoadPrecentTxt.gameObject.SetActive(active);
  467. }
  468. }