Entry.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using AppUI;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.Networking;
  8. using UnityEngine.SceneManagement;
  9. /* 程序启动入口 */
  10. public class Entry : MonoBehaviour
  11. {
  12. void Awake()
  13. {
  14. GameObject.Find("Canvas").GetComponent<Canvas>().enabled = true;
  15. //GameObject.Find("CanvasLogo").GetComponent<Canvas>().enabled = true;
  16. //设置新装app初始语言
  17. int curAppLanguage = PlayerPrefs.GetInt("AppLanguage", -1);
  18. if (curAppLanguage != CommonConfig.AppLanguage) {
  19. PlayerPrefs.SetInt("AppLanguage", CommonConfig.AppLanguage);
  20. PlayerPrefs.SetInt("Language", CommonConfig.AppLanguage);
  21. Debug.Log("SetAppLanguage");
  22. }
  23. }
  24. void Start()
  25. {
  26. Screen.fullScreen = true;
  27. //启动时候先用竖屏
  28. //Screen.orientation = ScreenOrientation.Portrait;
  29. //Screen.orientation = ScreenOrientation.AutoRotation;//设置方向为自动(根据需要自动旋转屏幕朝向任何启用的方向。)
  30. //Screen.autorotateToLandscapeRight = true; //允许自动旋转到右横屏
  31. //Screen.autorotateToLandscapeLeft = true; //允许自动旋转到左横屏
  32. //Screen.autorotateToPortrait = false; //不允许自动旋转到纵向
  33. //Screen.autorotateToPortraitUpsideDown = false; //不允许自动旋转到纵向上下
  34. ScreenOrientationHelper.ApplyPortraitForBoot();
  35. #if UNITY_EDITOR
  36. StartCoroutine(ScreenOrientationHelper.SwitchToPortraitEditorAndWait());
  37. #endif
  38. Screen.sleepTimeout = SleepTimeout.NeverSleep; //睡眠时间为从不睡眠
  39. //游戏帧率设置,需要在项目设置的Quality中关闭对应画质的垂直同步(VSync选项)
  40. QualitySettings.vSyncCount = 0;
  41. Application.targetFrameRate = 60;
  42. //QualitySettings.vSyncCount = 1;
  43. // SetTip("Loading", Color.white, 56);
  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. bool appVersionCheckOK = false;
  58. IEnumerator CheckAppVersion() {
  59. // countStr1 += "a";
  60. // string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  61. // countStr1 += "b";
  62. // using (UnityWebRequest request = UnityWebRequest.Get(url)) {
  63. // countStr1 += "c";
  64. // yield return request.SendWebRequest();
  65. // countStr1 += "d";
  66. // if (request.result == UnityWebRequest.Result.Success) {
  67. // countStr1 += "e";
  68. // appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  69. // countStr1 += "f";
  70. // if (appVersionCheckOK) {
  71. // countStr1 += "g";
  72. // //等场景加载完再提示
  73. // } else {
  74. // countStr1 += "h";
  75. // SetTip("请安装最新版本软件", Color.yellow);
  76. // }
  77. // } else {
  78. // countStr1 += "i";
  79. // SetTip("读取服务端软件版本配置失败", Color.red);
  80. // }
  81. // countStr1 += "j";
  82. // }
  83. // countStr1 += "k";
  84. // Debug.Log("countStr1:" + countStr1);
  85. yield return null;
  86. appVersionCheckOK = true;
  87. }
  88. float timeOnStartLoadScene;
  89. /// <summary>冷启动有本地 Token 时先 HTTP 校验,避免无效 Token 先进 Home。</summary>
  90. IEnumerator ValidateStoredLoginToken(Action<bool, LoginMgr.BootLoginTipKind> onDone)
  91. {
  92. RequestResult res = null;
  93. // Entry 场景名服务端不认,须传目标场景 Home(与旧版进 Home 后再 LoginByToken 一致)
  94. yield return LoginController.Instance.LoginByToken(r => res = r, "Home");
  95. if (res.code == 0)
  96. {
  97. string loginToken = (string)res.data;
  98. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  99. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  100. onDone(true, LoginMgr.BootLoginTipKind.None);
  101. }
  102. else if (res.code == -9999)
  103. {
  104. onDone(false, LoginMgr.BootLoginTipKind.NetworkError);
  105. }
  106. else
  107. {
  108. Debug.LogWarning($"[Entry] LoginByToken failed code={res.code} msg={res.msg}");
  109. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  110. CommonConfig.businessServerWsURL = null;
  111. onDone(false, LoginMgr.BootLoginTipKind.AuthExpired);
  112. }
  113. }
  114. IEnumerator AsyncLoadScene() {
  115. countStr2 += "a";
  116. timeOnStartLoadScene = Time.realtimeSinceStartup;
  117. //加载场景名
  118. string sceneName = "Login";
  119. if (CommonConfig.StandaloneMode)
  120. {
  121. sceneName = "Home";
  122. }
  123. else if (LoginMgr.HasToken())
  124. {
  125. bool tokenValid = false;
  126. LoginMgr.BootLoginTipKind failTip = LoginMgr.BootLoginTipKind.None;
  127. yield return ValidateStoredLoginToken((ok, tip) =>
  128. {
  129. tokenValid = ok;
  130. failTip = tip;
  131. });
  132. if (tokenValid)
  133. sceneName = "Home";
  134. else
  135. {
  136. sceneName = "Login";
  137. LoginMgr.PendingBootLoginTip = failTip;
  138. }
  139. }
  140. countStr2 += sceneName;
  141. //异步加载场景
  142. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  143. countStr2 += "b";
  144. //阻止当加载完成自动切换
  145. async.allowSceneActivation = false;
  146. countStr2 += "c";
  147. while (!async.isDone) {
  148. if (async.progress >= 0.9f) {
  149. countStr2 += "e";
  150. break;
  151. }
  152. if (!countStr2.Contains("d")) countStr2 += "d";
  153. yield return null;
  154. }
  155. countStr2 += "f";
  156. while (!appVersionCheckOK) {
  157. if (!countStr2.Contains("@")) countStr2 += "@";
  158. yield return null;
  159. }
  160. countStr2 += "g";
  161. while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) {
  162. if (!countStr2.Contains("%")) countStr2 += "%";
  163. yield return null;
  164. }
  165. countStr2 += "h";
  166. if (sceneName == "Home")
  167. yield return ScreenOrientationHelper.SwitchToLandscapeAndWait();
  168. if (sceneName == "Home")
  169. ScreenOrientationHelper.ScheduleFadeOutBlackOverlayAfterSceneLoad("Home");
  170. async.allowSceneActivation = true;
  171. countStr2 += "i";
  172. Debug.Log("countStr2:" + countStr2);
  173. //SetTip("软件版本校验成功", Color.green);
  174. }
  175. [SerializeField] Text tipText;
  176. void SetTip(string text, Color color, int fontSize = 40) {
  177. tipText.text = text;
  178. tipText.color = color;
  179. tipText.fontSize = fontSize;
  180. }
  181. int counter = 0;
  182. string countStr1 = "";
  183. string countStr2 = "";
  184. void OnGUI()
  185. {
  186. if (CommonConfig.ReleaseVersion2) return;
  187. GUIStyle labelFont = new GUIStyle();
  188. labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  189. labelFont.fontSize = 40;
  190. GUI.Label(new Rect(Screen.width/10,Screen.height/10,200,200), (counter++) + "," + countStr1 + "," + countStr2, labelFont);
  191. }
  192. }