Entry.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //初始化
  19. TextAutoLanguage.Init();
  20. SetTip("正在检测对比软件版本", Color.white);
  21. StartCoroutine(CheckAppVersion());
  22. StartCoroutine(AsyncLoadScene());
  23. }
  24. const string appVersion = "0.0.1";
  25. bool appVersionCheckOK = false;
  26. IEnumerator CheckAppVersion() {
  27. string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion;
  28. UnityWebRequest request = UnityWebRequest.Get(url);
  29. yield return request.SendWebRequest();
  30. if (request.result == UnityWebRequest.Result.Success) {
  31. appVersionCheckOK = bool.Parse(request.downloadHandler.text);
  32. if (appVersionCheckOK) {
  33. //等场景加载完再提示
  34. } else {
  35. SetTip("请安装最新版本软件", Color.yellow);
  36. DelayQuit();
  37. }
  38. } else {
  39. SetTip("读取服务端软件版本配置失败", Color.red);
  40. DelayQuit();
  41. }
  42. }
  43. IEnumerator AsyncLoadScene() {
  44. //加载场景名
  45. string sceneName = "Login";
  46. if (LoginMgr.HasToken()) {
  47. sceneName = "Home";
  48. }
  49. //异步加载场景
  50. AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single);
  51. //阻止当加载完成自动切换
  52. async.allowSceneActivation = false;
  53. while (!async.isDone) {
  54. if (async.progress >= 0.9f) {
  55. if (appVersionCheckOK) {
  56. async.allowSceneActivation = true;
  57. SetTip("软件版本校验成功", Color.green);
  58. break;
  59. }
  60. }
  61. yield return null;
  62. }
  63. }
  64. [SerializeField] Text tipText;
  65. void SetTip(string text, Color color) {
  66. tipText.text = text;
  67. tipText.color = color;
  68. }
  69. void DelayQuit() {
  70. DoTweenUtil.CallDelay(3f, () => {
  71. Application.Quit();
  72. });
  73. }
  74. }