Entry.cs 3.0 KB

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