LaunchLoadMgr.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. //m_LoadingRootGo.SetActive(false);
  150. //SetLoadEffectShowState(false);
  151. SetLoadDes("");
  152. SetLoadPrecent(0);
  153. FairGuard.Init();
  154. // 加载配置
  155. EventMgr.AddEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnConfigMgrInited);
  156. ConfigMgr.CreateInstance();
  157. }
  158. private void StartUILanuchAnim(int timerSequence)
  159. {
  160. TimerManager.Instance.RemoveTimer(timerSequence);
  161. m_TimerId = 0;
  162. m_LoginAnimator.enabled = true;
  163. m_LoginAnimator.Play("LoginShow", -1);
  164. m_LoginAnimator.Update(0);
  165. m_TimerId = TimerManager.Instance.AddTimer(2000, 1, OnLoadUILanuchAnimCompleted);
  166. }
  167. private void OnLoadUILanuchAnimCompleted(int timerSequence)
  168. {
  169. TimerManager.Instance.RemoveTimer(timerSequence);
  170. m_TimerId = 0;
  171. m_LoadingRootGo.SetActive(true);
  172. SetLoadEffectShowState(true);
  173. GameMgr.Instance.DoTaskByCorutine(CheckResUpdate);
  174. }
  175. private void OnConfigMgrInited(CoreEvent<int> ce)
  176. {
  177. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_ConfigMgrInit, OnConfigMgrInited);
  178. // m_LoadingRootGo.SetActive(true);
  179. //SetLoadEffectShowState(true);
  180. SetLoadDes("GameLogin17");
  181. SetLoadPrecent(10);
  182. if (m_AppVolLocalizeScript)
  183. m_AppVolLocalizeScript.OnChangeLang();
  184. if (m_ResVolLocalizeScript)
  185. m_ResVolLocalizeScript.OnChangeLang();
  186. int result = ce.Data;
  187. if (result == 1)
  188. {
  189. EventMgr.AddEventListener<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, OnLoadLuaOk);
  190. BattleFormulaParamMgr.CreateInstance();
  191. LuaMgr.Instance.InitMgr();
  192. }
  193. else
  194. {
  195. DebugHelper.LogError("加载配置文件出错,请check!!!");
  196. }
  197. }
  198. private void OnLoadLuaOk(CoreEvent<bool, float> ce)
  199. {
  200. bool result = ce.Data;
  201. int precent = Mathf.FloorToInt(Mathf.Clamp01(ce.Data1) * 80) + 10;
  202. SetLoadPrecent(precent);
  203. if (result)
  204. {
  205. EventMgr.RemoveEventListener<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, OnLoadLuaOk);
  206. SetLoadDes("GameLogin17");
  207. NetworkMgr.CreateInstance();
  208. ModelMgr.Instance.InitMgr();
  209. TowerBattleCfgMgr.CreateInstance();
  210. ResourceMgr.Instance.ReadResidentResCfg();
  211. EventMgr.AddEventListener<int>(ECoreEventType.EID_LOADKEY_OK, OnLoadKeyOk);
  212. SecurityLayer.Instance.Init();
  213. }
  214. }
  215. private void OnLoadKeyOk(CoreEvent<int> ce)
  216. {
  217. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_LOADKEY_OK, OnLoadKeyOk);
  218. SetLoadDes("GameLogin17");
  219. SetLoadPrecent(90);
  220. int result = ce.Data;
  221. if (result == 1)
  222. {
  223. EventMgr.AddEventListener<int>(ECoreEventType.EID_CameraMgrInited, OnCameraMgrInited);
  224. CameraMgr.Instance.InitMgr();
  225. }
  226. else
  227. {
  228. DebugHelper.LogError("加载Security文件出错,请check!!!");
  229. }
  230. }
  231. private void OnCameraMgrInited(CoreEvent<int> ce)
  232. {
  233. EventMgr.RemoveEventListener<int>(ECoreEventType.EID_CameraMgrInited, OnCameraMgrInited);
  234. SetLoadDes("GameLogin17");
  235. SetLoadPrecent(95);
  236. int result = ce.Data;
  237. if (result == 1)
  238. {
  239. ResourceMgr.Instance.LoadAsset<GameObject>(OnLoadClickEffectCallback, Constants.UIEffectPath, "FX_UI_DianJi");
  240. }
  241. else
  242. {
  243. DebugHelper.LogError("Camera 初始化失败,请check!!!");
  244. }
  245. }
  246. private void OnLoadClickEffectCallback(GameObject sourceGo, string path_, string[] assetNames_)
  247. {
  248. SetLoadDes("GameLogin17");
  249. SetLoadPrecent(99);
  250. GameObject go = GameObject.Instantiate(sourceGo);
  251. Transform trans = go.transform;
  252. trans.SetParent(UIMgr.Instance.ClickEffectTrans);
  253. trans.localPosition = Vector3.zero;
  254. trans.localRotation = Quaternion.identity;
  255. trans.localScale = Vector3.one;
  256. ResourceMgr.Instance.LoadDirAsset<List<GameObject>>(OnLoadUICommonPrefabCallback, Constants.UICommonPath);
  257. }
  258. private void OnLoadUICommonPrefabCallback(List<GameObject> objs, string path_, string[] assetNames_)
  259. {
  260. SetLoadDes("GameLogin17");
  261. SetLoadPrecent(100);
  262. m_IsCompleted = true;
  263. StopLaunch();
  264. }
  265. private GameObject m_LanuchGo;
  266. private GameObject m_LogoGo;
  267. private GameObject m_LoginRootGo;
  268. private Animator m_LoginAnimator;
  269. private GameObject m_LoadingRootGo;
  270. private Text m_LoadDesTxt;
  271. private Text m_LoadPrecentTxt;
  272. private Scrollbar m_LoadScrollBar;
  273. private GameObject m_LoadEffectGo;
  274. private TMPro.TextMeshProUGUI m_AppVolTxt;
  275. private UILocalizeScript m_AppVolLocalizeScript;
  276. private TMPro.TextMeshProUGUI m_ResVolTxt;
  277. private UILocalizeScript m_ResVolLocalizeScript;
  278. private GameObject m_EditionGo;
  279. private void InitUILanuch(GameObject sourceGo)
  280. {
  281. // 这里初始化保持和UIBase一致,方便后续对象池回收并使用
  282. GameObject pageBody = new GameObject("UILanuch");
  283. Canvas canvas = pageBody.AddComponent<Canvas>();
  284. pageBody.AddComponent<GraphicRaycaster>();
  285. CanvasGroup canvasGroup = pageBody.AddComponent<CanvasGroup>();
  286. if (canvasGroup != null)
  287. canvasGroup.interactable = false;
  288. pageBody.layer = LayerMask.NameToLayer("UI");
  289. var pageBodyTrans = pageBody.transform;
  290. pageBodyTrans.SetParent(UIMgr.Instance.UIRootTrans);
  291. canvas.overrideSorting = true;
  292. RectTransform rectTrans = pageBody.GetComponent<RectTransform>();
  293. if (rectTrans != null)
  294. {
  295. rectTrans.localPosition = Vector3.zero;
  296. rectTrans.localScale = Vector3.one;
  297. rectTrans.anchorMin = new Vector2(0, 0);
  298. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  299. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  300. rectTrans.anchoredPosition = Vector2.zero;
  301. rectTrans.sizeDelta = Vector2.zero;
  302. }
  303. GameObject rootRage = GameObject.Instantiate(sourceGo);
  304. rootRage.name = "Root";
  305. rootRage.SetActive(true);
  306. CommonUtil.SetGameObjectLayer(rootRage, "UI");
  307. rectTrans = rootRage.GetComponent<RectTransform>();
  308. rectTrans.SetParent(pageBodyTrans, false);
  309. rectTrans.localScale = Vector3.one;
  310. rectTrans.anchorMin = new Vector2(0, 0);
  311. rectTrans.anchorMax = new Vector2(1.0f, 1.0f);
  312. rectTrans.pivot = new Vector2(0.5f, 0.5f);
  313. rectTrans.anchoredPosition3D = Vector3.zero;
  314. rectTrans.sizeDelta = Vector2.zero;
  315. m_LanuchGo = pageBody;
  316. Transform rootTrans = rectTrans.Find("LoginAnim");
  317. m_LoginAnimator = rootTrans.GetComponent<Animator>();
  318. m_LogoGo = rootTrans.Find("Logo").gameObject;
  319. m_LoginRootGo = rootTrans.Find("LoginRoot").gameObject;
  320. Transform loadingRootTrans = rootTrans.Find("LoadingRoot");
  321. m_LoadingRootGo = loadingRootTrans.gameObject;
  322. Transform LoadingBarTrans = loadingRootTrans.Find("LoadingBar");
  323. m_LoadDesTxt = LoadingBarTrans.Find("Text").GetComponent<Text>();
  324. m_LoadPrecentTxt = LoadingBarTrans.Find("NumberText").GetComponent<Text>();
  325. Transform scrollBarTrans = LoadingBarTrans.Find("Scrollbar");
  326. m_LoadScrollBar = scrollBarTrans.GetComponent<Scrollbar>();
  327. m_LoadEffectGo = scrollBarTrans.Find("Sliding Area/Handle/Particle").gameObject;
  328. Transform volTrans = rootTrans.Find("Common/AppVol");
  329. m_AppVolTxt = volTrans.Find("ContentTxt").GetComponent<TMPro.TextMeshProUGUI>();
  330. m_AppVolLocalizeScript = volTrans.Find("NameTxt").GetComponent<UILocalizeScript>();
  331. volTrans = rootTrans.Find("Common/ResVol");
  332. m_ResVolTxt = volTrans.Find("ContentTxt").GetComponent<TMPro.TextMeshProUGUI>();
  333. m_ResVolLocalizeScript = volTrans.Find("NameTxt").GetComponent<UILocalizeScript>();
  334. m_EditionGo = rootTrans.Find("Common/EditionTxt").gameObject;
  335. m_AppVolTxt.text = GameMgr.Instance.GameVersion;
  336. m_ResVolTxt.text = GameMgr.Instance.ResVersion;
  337. m_LogoGo.SetActive(true);
  338. m_LoginRootGo.SetActive(false);
  339. m_LoadingRootGo.SetActive(false);
  340. #if HEALTH_BULLETIN
  341. m_EditionGo.SetActive(true);
  342. #else
  343. m_EditionGo.SetActive(false);
  344. #endif
  345. SetLoadEffectShowState(false);
  346. SetLoadDes("");
  347. SetLoadPrecent(0);
  348. }
  349. private void DisposeUILanuch()
  350. {
  351. if (m_LanuchGo)
  352. {
  353. ResourceMgr.Instance.RecycleUIGO(Constants.UIPath, "UILanuch/UILanuch_body", m_LanuchGo);
  354. m_LanuchGo = null;
  355. }
  356. m_LogoGo = null;
  357. m_LoginRootGo = null;
  358. m_LoadingRootGo = null;
  359. m_EditionGo = null;
  360. m_LoadDesTxt = null;
  361. m_LoadPrecentTxt = null;
  362. m_LoadScrollBar = null;
  363. m_LoadEffectGo = null;
  364. m_AppVolTxt = null;
  365. m_AppVolLocalizeScript = null;
  366. m_ResVolTxt = null;
  367. m_ResVolLocalizeScript = null;
  368. }
  369. private void SetLoadEffectShowState(bool state)
  370. {
  371. m_LoadEffectGo.SetActive(state);
  372. }
  373. private void SetLoadDes(string langKey)
  374. {
  375. if (m_LoadDesTxt)
  376. {
  377. if (string.IsNullOrEmpty(langKey))
  378. m_LoadDesTxt.text = "";
  379. else
  380. m_LoadDesTxt.text = I18N.T(langKey);
  381. }
  382. }
  383. private void SetLoadPrecent(int precent)
  384. {
  385. precent = Mathf.Clamp(precent, 0, 100);
  386. if (m_LoadPrecentTxt)
  387. m_LoadPrecentTxt.text = precent + "%";
  388. if (m_LoadScrollBar)
  389. m_LoadScrollBar.size = precent * 0.01f;
  390. }
  391. }