Entry.cs 5.6 KB

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