Entry.cs 5.5 KB

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