Entry.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Awake()
  11. {
  12. #if UNITY_IOS
  13. if (PlayerPrefs.GetInt("CanGoToApp", 0) == 0)
  14. {
  15. Destroy(gameObject);
  16. SceneManager.LoadScene("HeartRateBand");
  17. return;
  18. }
  19. #endif
  20. GameObject.Find("Canvas").GetComponent<Canvas>().enabled = true;
  21. GameObject.Find("CanvasLogo").GetComponent<Canvas>().enabled = true;
  22. //设置新装app初始语言
  23. int curAppLanguage = PlayerPrefs.GetInt("AppLanguage", -1);
  24. if (curAppLanguage != CommonConfig.AppLanguage) {
  25. PlayerPrefs.SetInt("AppLanguage", CommonConfig.AppLanguage);
  26. PlayerPrefs.SetInt("Language", CommonConfig.AppLanguage);
  27. Debug.Log("SetAppLanguage");
  28. }
  29. }
  30. void Start()
  31. {
  32. Screen.orientation = ScreenOrientation.AutoRotation;//设置方向为自动(根据需要自动旋转屏幕朝向任何启用的方向。)
  33. Screen.autorotateToLandscapeRight = true; //允许自动旋转到右横屏
  34. Screen.autorotateToLandscapeLeft = true; //允许自动旋转到左横屏
  35. Screen.autorotateToPortrait = false; //不允许自动旋转到纵向
  36. Screen.autorotateToPortraitUpsideDown = false; //不允许自动旋转到纵向上下
  37. Screen.sleepTimeout = SleepTimeout.NeverSleep; //睡眠时间为从不睡眠
  38. //游戏帧率设置,需要在项目设置的Quality中关闭对应画质的垂直同步(VSync选项)
  39. // QualitySettings.vSyncCount = 0;
  40. // Application.targetFrameRate = 60;
  41. QualitySettings.vSyncCount = 1;
  42. // SetTip("Loading", Color.white, 56);
  43. versionText.text = "Version_" + Application.version;
  44. //SetTip("正在检测对比软件版本", Color.white);
  45. StartCoroutine(CheckAppVersion());
  46. StartCoroutine(AsyncLoadScene());
  47. SceneManager.sceneUnloaded += (scene) => {
  48. long t1 = JCUnityLib.TimeUtils.GetTimestamp();
  49. Resources.UnloadUnusedAssets();
  50. System.GC.Collect();
  51. long t2 = JCUnityLib.TimeUtils.GetTimestamp();
  52. Debug.Log($"场景{scene.name}销毁,释放资源和GC回收的总耗时={t2 - t1}ms");
  53. };
  54. PersistenHandler.Init();
  55. SimulateMouseController.Init();
  56. }
  57. [SerializeField] Text versionText;
  58. bool appVersionCheckOK = false;
  59. IEnumerator CheckAppVersion() {
  60. // countStr1 += "a";
  61. // string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  62. // countStr1 += "b";
  63. // using (UnityWebRequest request = UnityWebRequest.Get(url)) {
  64. // countStr1 += "c";
  65. // yield return request.SendWebRequest();
  66. // countStr1 += "d";
  67. // if (request.result == UnityWebRequest.Result.Success) {
  68. // countStr1 += "e";
  69. // appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  70. // countStr1 += "f";
  71. // if (appVersionCheckOK) {
  72. // countStr1 += "g";
  73. // //等场景加载完再提示
  74. // } else {
  75. // countStr1 += "h";
  76. // SetTip("请安装最新版本软件", Color.yellow);
  77. // }
  78. // } else {
  79. // countStr1 += "i";
  80. // SetTip("读取服务端软件版本配置失败", Color.red);
  81. // }
  82. // countStr1 += "j";
  83. // }
  84. // countStr1 += "k";
  85. // Debug.Log("countStr1:" + countStr1);
  86. yield return null;
  87. appVersionCheckOK = true;
  88. }
  89. float timeOnStartLoadScene;
  90. IEnumerator AsyncLoadScene() {
  91. countStr2 += "a";
  92. timeOnStartLoadScene = Time.realtimeSinceStartup;
  93. //加载场景名
  94. string sceneName = "Login";
  95. if (LoginMgr.HasToken()) {
  96. sceneName = "Home";
  97. }
  98. countStr2 += sceneName;
  99. //异步加载场景
  100. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  101. countStr2 += "b";
  102. //阻止当加载完成自动切换
  103. async.allowSceneActivation = false;
  104. countStr2 += "c";
  105. while (!async.isDone) {
  106. if (async.progress >= 0.9f) {
  107. countStr2 += "e";
  108. break;
  109. }
  110. if (!countStr2.Contains("d")) countStr2 += "d";
  111. yield return null;
  112. }
  113. countStr2 += "f";
  114. while (!appVersionCheckOK) {
  115. if (!countStr2.Contains("@")) countStr2 += "@";
  116. yield return null;
  117. }
  118. countStr2 += "g";
  119. while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) {
  120. if (!countStr2.Contains("%")) countStr2 += "%";
  121. yield return null;
  122. }
  123. countStr2 += "h";
  124. async.allowSceneActivation = true;
  125. countStr2 += "i";
  126. Debug.Log("countStr2:" + countStr2);
  127. //SetTip("软件版本校验成功", Color.green);
  128. }
  129. [SerializeField] Text tipText;
  130. void SetTip(string text, Color color, int fontSize = 40) {
  131. tipText.text = text;
  132. tipText.color = color;
  133. tipText.fontSize = fontSize;
  134. }
  135. int counter = 0;
  136. string countStr1 = "";
  137. string countStr2 = "";
  138. void OnGUI()
  139. {
  140. if (CommonConfig.ReleaseVersion2) return;
  141. GUIStyle labelFont = new GUIStyle();
  142. labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  143. labelFont.fontSize = 40;
  144. GUI.Label(new Rect(Screen.width/10,Screen.height/10,200,200), (counter++) + "," + countStr1 + "," + countStr2, labelFont);
  145. }
  146. }