LaunchLoadMgr.cs 15 KB

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