LaunchLoadMgr.cs 14 KB

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