Entry.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Start()
  11. {
  12. Screen.orientation = ScreenOrientation.AutoRotation;//设置方向为自动(根据需要自动旋转屏幕朝向任何启用的方向。)
  13. Screen.autorotateToLandscapeRight = true; //允许自动旋转到右横屏
  14. Screen.autorotateToLandscapeLeft = true; //允许自动旋转到左横屏
  15. Screen.autorotateToPortrait = false; //不允许自动旋转到纵向
  16. Screen.autorotateToPortraitUpsideDown = false; //不允许自动旋转到纵向上下
  17. Screen.sleepTimeout = SleepTimeout.NeverSleep; //睡眠时间为从不睡眠
  18. //游戏帧率设置,需要在项目设置的Quality中关闭对应画质的垂直同步(VSync选项)
  19. // QualitySettings.vSyncCount = 0;
  20. // Application.targetFrameRate = 60;
  21. QualitySettings.vSyncCount = 1;
  22. SetTip("Loading", Color.white, 56);
  23. versionText.text = "Version_" + appVersion + "_R3";
  24. //SetTip("正在检测对比软件版本", Color.white);
  25. StartCoroutine(CheckAppVersion());
  26. StartCoroutine(AsyncLoadScene());
  27. }
  28. [SerializeField] Text versionText;
  29. const string appVersion = "0.0.1";
  30. bool appVersionCheckOK = false;
  31. IEnumerator CheckAppVersion() {
  32. string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  33. UnityWebRequest request = UnityWebRequest.Get(url);
  34. yield return request.SendWebRequest();
  35. if (request.result == UnityWebRequest.Result.Success) {
  36. appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  37. if (appVersionCheckOK) {
  38. //等场景加载完再提示
  39. } else {
  40. SetTip("请安装最新版本软件", Color.yellow);
  41. DelayQuit();
  42. }
  43. } else {
  44. SetTip("读取服务端软件版本配置失败", Color.red);
  45. DelayQuit();
  46. }
  47. }
  48. float timeOnStartLoadScene;
  49. IEnumerator AsyncLoadScene() {
  50. timeOnStartLoadScene = Time.realtimeSinceStartup;
  51. //加载场景名
  52. string sceneName = "Login";
  53. if (LoginMgr.HasToken()) {
  54. sceneName = "Home";
  55. }
  56. //异步加载场景
  57. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  58. //阻止当加载完成自动切换
  59. async.allowSceneActivation = false;
  60. while (!async.isDone) {
  61. if (async.progress >= 0.9f) {
  62. break;
  63. }
  64. yield return null;
  65. }
  66. while (!appVersionCheckOK) {
  67. yield return null;
  68. }
  69. while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) {
  70. yield return null;
  71. }
  72. async.allowSceneActivation = true;
  73. //SetTip("软件版本校验成功", Color.green);
  74. }
  75. [SerializeField] Text tipText;
  76. void SetTip(string text, Color color, int fontSize = 40) {
  77. tipText.text = text;
  78. tipText.color = color;
  79. tipText.fontSize = fontSize;
  80. }
  81. void DelayQuit() {
  82. DoTweenUtil.CallDelay(3f, () => {
  83. Application.Quit();
  84. });
  85. }
  86. }