Entry.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. using UnityEngine.SceneManagement;
  7. /* 程序启动入口 */
  8. public class Entry : MonoBehaviour
  9. {
  10. void Start()
  11. {
  12. Screen.orientation = ScreenOrientation.AutoRotation;//设置方向为自动(根据需要自动旋转屏幕朝向任何启用的方向。)
  13. Screen.autorotateToLandscapeRight = true; //允许自动旋转到右横屏
  14. Screen.autorotateToLandscapeLeft = true; //允许自动旋转到左横屏
  15. Screen.autorotateToPortrait = false; //不允许自动旋转到纵向
  16. Screen.autorotateToPortraitUpsideDown = false; //不允许自动旋转到纵向上下
  17. Screen.sleepTimeout = SleepTimeout.NeverSleep; //睡眠时间为从不睡眠
  18. //游戏帧率设置,需要在项目设置的Quality中关闭对应画质的垂直同步(VSync选项)
  19. // QualitySettings.vSyncCount = 0;
  20. // Application.targetFrameRate = 60;
  21. QualitySettings.vSyncCount = 1;
  22. // SetTip("Loading", Color.white, 56);
  23. versionText.text = "Version_" + appVersion + "_R5";
  24. //SetTip("正在检测对比软件版本", Color.white);
  25. StartCoroutine(CheckAppVersion());
  26. StartCoroutine(AsyncLoadScene());
  27. SceneManager.sceneUnloaded += (scene) => {
  28. long t1 = JC.CS.Utility.GetTimestamp();
  29. Resources.UnloadUnusedAssets();
  30. System.GC.Collect();
  31. long t2 = JC.CS.Utility.GetTimestamp();
  32. Debug.Log($"场景{scene.name}销毁,释放资源和GC回收的总耗时={t2 - t1}ms");
  33. };
  34. }
  35. [SerializeField] Text versionText;
  36. const string appVersion = "0.0.1";
  37. bool appVersionCheckOK = false;
  38. IEnumerator CheckAppVersion() {
  39. string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  40. using (UnityWebRequest request = UnityWebRequest.Get(url)) {
  41. yield return request.SendWebRequest();
  42. if (request.result == UnityWebRequest.Result.Success) {
  43. appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  44. if (appVersionCheckOK) {
  45. //等场景加载完再提示
  46. } else {
  47. SetTip("请安装最新版本软件", Color.yellow);
  48. DelayQuit();
  49. }
  50. } else {
  51. SetTip("读取服务端软件版本配置失败", Color.red);
  52. DelayQuit();
  53. }
  54. }
  55. }
  56. float timeOnStartLoadScene;
  57. IEnumerator AsyncLoadScene() {
  58. timeOnStartLoadScene = Time.realtimeSinceStartup;
  59. //加载场景名
  60. string sceneName = "Login";
  61. if (LoginMgr.HasToken()) {
  62. sceneName = "Home";
  63. }
  64. //异步加载场景
  65. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  66. //阻止当加载完成自动切换
  67. async.allowSceneActivation = false;
  68. while (!async.isDone) {
  69. if (async.progress >= 0.9f) {
  70. break;
  71. }
  72. yield return null;
  73. }
  74. while (!appVersionCheckOK) {
  75. yield return null;
  76. }
  77. while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) {
  78. yield return null;
  79. }
  80. async.allowSceneActivation = true;
  81. //SetTip("软件版本校验成功", Color.green);
  82. }
  83. [SerializeField] Text tipText;
  84. void SetTip(string text, Color color, int fontSize = 40) {
  85. tipText.text = text;
  86. tipText.color = color;
  87. tipText.fontSize = fontSize;
  88. }
  89. void DelayQuit() {
  90. DoTweenUtil.CallDelay(3f, () => {
  91. Application.Quit();
  92. });
  93. }
  94. }