LaunchLoadMgr.cs 17 KB

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