LaunchLoadMgr.cs 17 KB

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