LauncherController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using GFGGame.Launcher;
  3. using UnityEngine;
  4. using UniFramework.Event;
  5. namespace GFGGame
  6. {
  7. public class LauncherController
  8. {
  9. private static EventGroup eventGroup = new EventGroup();
  10. /// <summary>
  11. /// 初始化启动器配置
  12. /// </summary>
  13. public static void InitLauncherCfg()
  14. {
  15. LauncherView.Instance.SetDesc("正在检查更新...");
  16. var url = LauncherConfig.cfgUrl.Replace("{cfgName}", LauncherConfig.cfgName);
  17. url = url + "?t=" + DateTime.Now.Ticks;
  18. HttpTool.Instance.Get(url, (string json) =>
  19. {
  20. LauncherConfig.InitPlatform(json);
  21. CheckShowAgreeView(CheckGameStatus);
  22. });
  23. }
  24. private static void CheckShowAgreeView(Action onSuccess)
  25. {
  26. if (LocalCache.GetBool(LauncherConfig.LAST_LOGIN_IS_AGREE_KEY, false))
  27. {
  28. onSuccess?.Invoke();
  29. }
  30. else
  31. {
  32. LauncherAgreeView.Instance.Open(() => {
  33. onSuccess?.Invoke();
  34. });
  35. }
  36. }
  37. public static void CheckGameStatus()
  38. {
  39. if (LauncherConfig.serverStatus == 1)
  40. {
  41. if(string.IsNullOrEmpty(LauncherConfig.statusPrompt))
  42. {
  43. LauncherConfig.statusPrompt = "游戏正在维护中,请稍后再试。";
  44. }
  45. Alert.Show(LauncherConfig.statusPrompt)
  46. .SetLeftButton(true, "知道了", (data) =>
  47. {
  48. Application.Quit();
  49. });
  50. }
  51. else
  52. {
  53. InitSDK();
  54. }
  55. }
  56. private static void InitSDK()
  57. {
  58. InitReporter();
  59. InitBugly();
  60. eventGroup.AddListener<LauncherEvent.InitPlatformResult>(OnInitPlatform);
  61. QDManagerInit.InitPlatform();
  62. }
  63. private static void OnInitPlatform(IEventMessage obj)
  64. {
  65. LauncherEvent.InitPlatformResult initPlatformResult = obj as LauncherEvent.InitPlatformResult;
  66. if (initPlatformResult != null)
  67. {
  68. if(initPlatformResult.success)
  69. {
  70. OnInitSDKSuccess();
  71. }
  72. else
  73. {
  74. OnInitSDKFail();
  75. }
  76. }
  77. }
  78. private static void OnInitSDKFail()
  79. {
  80. Alert.Show("初始化平台sdk失败!")
  81. .SetRightButton(true, "重试", (t) => { QDManagerInit.InitPlatform(); });
  82. }
  83. private static void OnInitSDKSuccess()
  84. {
  85. #if UNITY_EDITOR
  86. InitResVersion();
  87. #else
  88. CheckApkVersion();
  89. #endif
  90. }
  91. private static void CheckApkVersion()
  92. {
  93. var versionTarget = LauncherConfig.apkVersion;
  94. var version = Application.version;
  95. if (VersionUtil.compare(version, versionTarget))
  96. {
  97. DownloadApk();
  98. }
  99. else
  100. {
  101. InitResVersion();
  102. }
  103. }
  104. private static void DownloadApk()
  105. {
  106. if(string.IsNullOrEmpty(LauncherConfig.updateAppPrompt))
  107. {
  108. LauncherConfig.updateAppPrompt = "需要安装新的安装包,请联系研发获取。";
  109. }
  110. Alert.Show(LauncherConfig.updateAppPrompt)
  111. .SetLeftButton(true, "知道了", (data) =>
  112. {
  113. Application.Quit();
  114. });
  115. }
  116. private static void InitResVersion()
  117. {
  118. VersionController.Instance.Init();
  119. }
  120. public static void OnVersionCompleted()
  121. {
  122. StartGame();
  123. }
  124. private static void StartGame()
  125. {
  126. LauncherView.Instance.SetDesc($"正在初始化...");
  127. HotUpdateCodeLoader.Instance.StartLoad();
  128. }
  129. private static void InitReporter()
  130. {
  131. Reporter reporter = GameObject.Find("Reporter").GetComponent<Reporter>();
  132. reporter.numOfCircleToShow = 10;
  133. reporter.isOpen = LauncherConfig.onDebug > 0;
  134. }
  135. private static void InitBugly()
  136. {
  137. BuglyAgent.ConfigDebugMode(true);
  138. // 注册日志回调,替换使用 'Application.RegisterLogCallback(Application.LogCallback)'注册日志回调的方式
  139. // BuglyAgent.RegisterLogCallback (CallbackDelegate.Instance.OnApplicationLogCallbackHandler);
  140. #if UNITY_IPHONE || UNITY_IOS
  141. BuglyAgent.InitWithAppId ("f8cf2b57f3");
  142. #elif UNITY_ANDROID
  143. if (LauncherConfig.netType == LauncherConfig.EnumNetType.LOCAL)
  144. {
  145. BuglyAgent.InitWithAppId("766c5bdb0f");
  146. }
  147. else if (LauncherConfig.netType == LauncherConfig.EnumNetType.PUBLIC)
  148. {
  149. BuglyAgent.InitWithAppId("b6d0b1b8c5");
  150. }
  151. #endif
  152. // 如果你确认已在对应的iOS工程或Android工程中初始化SDK,那么在脚本中只需启动C#异常捕获上报功能即可
  153. BuglyAgent.EnableExceptionHandler();
  154. }
  155. }
  156. }