GameLauncher.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. using GFGGame;
  3. using ET;
  4. using System.Threading;
  5. using GFGGame.Launcher;
  6. using FairyGUI;
  7. public class GameLauncher : MonoBehaviour
  8. {
  9. private void Awake()
  10. {
  11. //ET
  12. System.AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  13. {
  14. Log.Error(e.ExceptionObject.ToString());
  15. };
  16. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  17. DontDestroyOnLoad(gameObject);
  18. LitJson.UnityTypeBindings.Register();
  19. ETTask.ExceptionHandler += Log.Error;
  20. Log.ILog = new UnityLogger();
  21. Options.Instance = new Options();
  22. TimeInfo.Instance.TimeZone = 8;
  23. }
  24. // Start is called before the first frame update
  25. void Start()
  26. {
  27. InitBugly();
  28. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  29. Application.runInBackground = true;
  30. LauncherConfig.InitScriptCompilation();
  31. FGUILauncher.Init();
  32. HealthAdviceView.Instance.Open();
  33. int time = LauncherConfig.netType == LauncherConfig.EnumNetType.LOCAL ? 1 : 10;
  34. Timers.inst.Add(time, 1, (object param) =>
  35. {
  36. HealthAdviceView.Instance.Close();
  37. LauncherView.Instance.Open();
  38. InitLauncherCfg();
  39. });
  40. }
  41. /// <summary>
  42. /// 初始化启动器配置
  43. /// </summary>
  44. public void InitLauncherCfg()
  45. {
  46. LauncherView.Instance.SetDesc("正在初始化..");
  47. var url = LauncherConfig.cfgUrl.Replace("{cfgName}", LauncherConfig.cfgName);
  48. url = url + "?t=" + TimeHelper.ClientNow();
  49. HttpTool.Instance.Get(url, (string json) =>
  50. {
  51. LauncherConfig.InitPlatform(json);
  52. LauncherView.Instance.SetDesc("正在初始化...");
  53. //第一个节点必须放在平台配置加载之后
  54. LogServerHelperHttp.SendNodeLog((int)LogNode.OnStart);
  55. CheckGameStatus();
  56. });
  57. }
  58. public void CheckGameStatus()
  59. {
  60. if (LauncherConfig.serverStatus == 1)
  61. {
  62. Alert.Show("游戏正在维护中,请稍后再试。")
  63. .SetLeftButton(true, "知道了", (data) =>
  64. {
  65. Application.Quit();
  66. });
  67. }
  68. else
  69. {
  70. CheckApkVersion();
  71. }
  72. }
  73. private void CheckApkVersion()
  74. {
  75. LauncherView.Instance.SetDesc("正在校验应用版本...");
  76. var versionTarget = LauncherConfig.apkVersion;
  77. var version = Application.version;
  78. if (VersionUtil.compare(version, versionTarget))
  79. {
  80. DownloadApk();
  81. }
  82. else
  83. {
  84. VersionController.Instance.Init();
  85. }
  86. }
  87. private void DownloadApk()
  88. {
  89. Alert.Show("需要安装新的安装包,请联系研发获取。")
  90. .SetLeftButton(true, "知道了", (data) =>
  91. {
  92. Application.Quit();
  93. });
  94. }
  95. private void InitBugly()
  96. {
  97. BuglyAgent.ConfigDebugMode(true);
  98. // 注册日志回调,替换使用 'Application.RegisterLogCallback(Application.LogCallback)'注册日志回调的方式
  99. // BuglyAgent.RegisterLogCallback (CallbackDelegate.Instance.OnApplicationLogCallbackHandler);
  100. #if UNITY_IPHONE || UNITY_IOS
  101. BuglyAgent.InitWithAppId (BuglyAppIDForiOS);
  102. #elif UNITY_ANDROID
  103. BuglyAgent.InitWithAppId("766c5bdb0f");
  104. #endif
  105. // 如果你确认已在对应的iOS工程或Android工程中初始化SDK,那么在脚本中只需启动C#异常捕获上报功能即可
  106. BuglyAgent.EnableExceptionHandler();
  107. }
  108. }