Entry.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 + "_R7";
  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. PersistenHandler.Init();
  35. SimulateMouseController.Init();
  36. }
  37. [SerializeField] Text versionText;
  38. const string appVersion = "0.0.1";
  39. bool appVersionCheckOK = false;
  40. IEnumerator CheckAppVersion() {
  41. countStr1 += "a";
  42. string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  43. countStr1 += "b";
  44. using (UnityWebRequest request = UnityWebRequest.Get(url)) {
  45. countStr1 += "c";
  46. yield return request.SendWebRequest();
  47. countStr1 += "d";
  48. if (request.result == UnityWebRequest.Result.Success) {
  49. countStr1 += "e";
  50. appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  51. countStr1 += "f";
  52. if (appVersionCheckOK) {
  53. countStr1 += "g";
  54. //等场景加载完再提示
  55. } else {
  56. countStr1 += "h";
  57. SetTip("请安装最新版本软件", Color.yellow);
  58. DelayQuit();
  59. }
  60. } else {
  61. countStr1 += "i";
  62. SetTip("读取服务端软件版本配置失败", Color.red);
  63. DelayQuit();
  64. }
  65. countStr1 += "j";
  66. }
  67. countStr1 += "k";
  68. Debug.Log("countStr1:" + countStr1);
  69. }
  70. float timeOnStartLoadScene;
  71. IEnumerator AsyncLoadScene() {
  72. countStr2 += "a";
  73. timeOnStartLoadScene = Time.realtimeSinceStartup;
  74. //加载场景名
  75. string sceneName = "Login";
  76. if (LoginMgr.HasToken()) {
  77. sceneName = "Home";
  78. }
  79. countStr2 += sceneName;
  80. //异步加载场景
  81. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  82. countStr2 += "b";
  83. //阻止当加载完成自动切换
  84. async.allowSceneActivation = false;
  85. countStr2 += "c";
  86. while (!async.isDone) {
  87. if (async.progress >= 0.9f) {
  88. countStr2 += "e";
  89. break;
  90. }
  91. if (!countStr2.Contains("d")) countStr2 += "d";
  92. yield return null;
  93. }
  94. countStr2 += "f";
  95. while (!appVersionCheckOK) {
  96. if (!countStr2.Contains("@")) countStr2 += "@";
  97. yield return null;
  98. }
  99. countStr2 += "g";
  100. while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) {
  101. if (!countStr2.Contains("%")) countStr2 += "%";
  102. yield return null;
  103. }
  104. countStr2 += "h";
  105. async.allowSceneActivation = true;
  106. countStr2 += "i";
  107. Debug.Log("countStr2:" + countStr2);
  108. //SetTip("软件版本校验成功", Color.green);
  109. }
  110. [SerializeField] Text tipText;
  111. void SetTip(string text, Color color, int fontSize = 40) {
  112. tipText.text = text;
  113. tipText.color = color;
  114. tipText.fontSize = fontSize;
  115. }
  116. void DelayQuit() {
  117. DoTweenUtil.CallDelay(3f, () => {
  118. Application.Quit();
  119. });
  120. }
  121. int counter = 0;
  122. string countStr1 = "";
  123. string countStr2 = "";
  124. void OnGUI()
  125. {
  126. GUIStyle labelFont = new GUIStyle();
  127. labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  128. labelFont.fontSize = 40;
  129. GUI.Label(new Rect(Screen.width/10,Screen.height/10,200,200), (counter++) + "," + countStr1 + "," + countStr2, labelFont);
  130. }
  131. }