using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using UnityEngine.SceneManagement; /* 程序启动入口 */ public class Entry : MonoBehaviour { void Start() { Screen.orientation = ScreenOrientation.AutoRotation;//设置方向为自动(根据需要自动旋转屏幕朝向任何启用的方向。) Screen.autorotateToLandscapeRight = true; //允许自动旋转到右横屏 Screen.autorotateToLandscapeLeft = true; //允许自动旋转到左横屏 Screen.autorotateToPortrait = false; //不允许自动旋转到纵向 Screen.autorotateToPortraitUpsideDown = false; //不允许自动旋转到纵向上下 Screen.sleepTimeout = SleepTimeout.NeverSleep; //睡眠时间为从不睡眠 //游戏帧率设置,需要在项目设置的Quality中关闭对应画质的垂直同步(VSync选项) // QualitySettings.vSyncCount = 0; // Application.targetFrameRate = 60; QualitySettings.vSyncCount = 1; // SetTip("Loading", Color.white, 56); versionText.text = "Version_" + appVersion + "_R5"; //SetTip("正在检测对比软件版本", Color.white); StartCoroutine(CheckAppVersion()); StartCoroutine(AsyncLoadScene()); SceneManager.sceneUnloaded += (scene) => { long t1 = JC.CS.Utility.GetTimestamp(); Resources.UnloadUnusedAssets(); System.GC.Collect(); long t2 = JC.CS.Utility.GetTimestamp(); Debug.Log($"场景{scene.name}销毁,释放资源和GC回收的总耗时={t2 - t1}ms"); }; } [SerializeField] Text versionText; const string appVersion = "0.0.1"; bool appVersionCheckOK = false; IEnumerator CheckAppVersion() { string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion; using (UnityWebRequest request = UnityWebRequest.Get(url)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { appVersionCheckOK = bool.Parse(request.downloadHandler.text); if (appVersionCheckOK) { //等场景加载完再提示 } else { SetTip("请安装最新版本软件", Color.yellow); DelayQuit(); } } else { SetTip("读取服务端软件版本配置失败", Color.red); DelayQuit(); } } } float timeOnStartLoadScene; IEnumerator AsyncLoadScene() { timeOnStartLoadScene = Time.realtimeSinceStartup; //加载场景名 string sceneName = "Login"; if (LoginMgr.HasToken()) { sceneName = "Home"; } //异步加载场景 AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single); //阻止当加载完成自动切换 async.allowSceneActivation = false; while (!async.isDone) { if (async.progress >= 0.9f) { break; } yield return null; } while (!appVersionCheckOK) { yield return null; } while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) { yield return null; } async.allowSceneActivation = true; //SetTip("软件版本校验成功", Color.green); } [SerializeField] Text tipText; void SetTip(string text, Color color, int fontSize = 40) { tipText.text = text; tipText.color = color; tipText.fontSize = fontSize; } void DelayQuit() { DoTweenUtil.CallDelay(3f, () => { Application.Quit(); }); } }