Entry.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 (CommonConfig.iosTaoKe && 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. //SetTip("正在检测对比软件版本", Color.white);
  44. StartCoroutine(CheckAppVersion());
  45. StartCoroutine(AsyncLoadScene());
  46. SceneManager.sceneUnloaded += (scene) => {
  47. long t1 = JCUnityLib.TimeUtils.GetTimestamp();
  48. Resources.UnloadUnusedAssets();
  49. System.GC.Collect();
  50. long t2 = JCUnityLib.TimeUtils.GetTimestamp();
  51. Debug.Log($"场景{scene.name}销毁,释放资源和GC回收的总耗时={t2 - t1}ms");
  52. };
  53. PersistenHandler.Init();
  54. SimulateMouseController.Init();
  55. }
  56. bool appVersionCheckOK = false;
  57. IEnumerator CheckAppVersion() {
  58. // countStr1 += "a";
  59. // string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  60. // countStr1 += "b";
  61. // using (UnityWebRequest request = UnityWebRequest.Get(url)) {
  62. // countStr1 += "c";
  63. // yield return request.SendWebRequest();
  64. // countStr1 += "d";
  65. // if (request.result == UnityWebRequest.Result.Success) {
  66. // countStr1 += "e";
  67. // appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  68. // countStr1 += "f";
  69. // if (appVersionCheckOK) {
  70. // countStr1 += "g";
  71. // //等场景加载完再提示
  72. // } else {
  73. // countStr1 += "h";
  74. // SetTip("请安装最新版本软件", Color.yellow);
  75. // }
  76. // } else {
  77. // countStr1 += "i";
  78. // SetTip("读取服务端软件版本配置失败", Color.red);
  79. // }
  80. // countStr1 += "j";
  81. // }
  82. // countStr1 += "k";
  83. // Debug.Log("countStr1:" + countStr1);
  84. yield return null;
  85. appVersionCheckOK = true;
  86. }
  87. float timeOnStartLoadScene;
  88. IEnumerator AsyncLoadScene() {
  89. countStr2 += "a";
  90. timeOnStartLoadScene = Time.realtimeSinceStartup;
  91. //加载场景名
  92. string sceneName = "Login";
  93. if (LoginMgr.HasToken()) {
  94. sceneName = "Home";
  95. }
  96. countStr2 += sceneName;
  97. //异步加载场景
  98. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  99. countStr2 += "b";
  100. //阻止当加载完成自动切换
  101. async.allowSceneActivation = false;
  102. countStr2 += "c";
  103. while (!async.isDone) {
  104. if (async.progress >= 0.9f) {
  105. countStr2 += "e";
  106. break;
  107. }
  108. if (!countStr2.Contains("d")) countStr2 += "d";
  109. yield return null;
  110. }
  111. countStr2 += "f";
  112. while (!appVersionCheckOK) {
  113. if (!countStr2.Contains("@")) countStr2 += "@";
  114. yield return null;
  115. }
  116. countStr2 += "g";
  117. while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) {
  118. if (!countStr2.Contains("%")) countStr2 += "%";
  119. yield return null;
  120. }
  121. countStr2 += "h";
  122. async.allowSceneActivation = true;
  123. countStr2 += "i";
  124. Debug.Log("countStr2:" + countStr2);
  125. //SetTip("软件版本校验成功", Color.green);
  126. }
  127. [SerializeField] Text tipText;
  128. void SetTip(string text, Color color, int fontSize = 40) {
  129. tipText.text = text;
  130. tipText.color = color;
  131. tipText.fontSize = fontSize;
  132. }
  133. int counter = 0;
  134. string countStr1 = "";
  135. string countStr2 = "";
  136. void OnGUI()
  137. {
  138. if (CommonConfig.ReleaseVersion2) return;
  139. GUIStyle labelFont = new GUIStyle();
  140. labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  141. labelFont.fontSize = 40;
  142. GUI.Label(new Rect(Screen.width/10,Screen.height/10,200,200), (counter++) + "," + countStr1 + "," + countStr2, labelFont);
  143. }
  144. }