using System; using System.Collections; using System.Collections.Generic; using AppUI; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using UnityEngine.SceneManagement; /* 程序启动入口 */ public class Entry : MonoBehaviour { void Awake() { GameObject.Find("Canvas").GetComponent().enabled = true; //GameObject.Find("CanvasLogo").GetComponent().enabled = true; //设置新装app初始语言 int curAppLanguage = PlayerPrefs.GetInt("AppLanguage", -1); if (curAppLanguage != CommonConfig.AppLanguage) { PlayerPrefs.SetInt("AppLanguage", CommonConfig.AppLanguage); PlayerPrefs.SetInt("Language", CommonConfig.AppLanguage); Debug.Log("SetAppLanguage"); } } void Start() { Screen.fullScreen = true; //启动时候先用竖屏 //Screen.orientation = ScreenOrientation.Portrait; //Screen.orientation = ScreenOrientation.AutoRotation;//设置方向为自动(根据需要自动旋转屏幕朝向任何启用的方向。) //Screen.autorotateToLandscapeRight = true; //允许自动旋转到右横屏 //Screen.autorotateToLandscapeLeft = true; //允许自动旋转到左横屏 //Screen.autorotateToPortrait = false; //不允许自动旋转到纵向 //Screen.autorotateToPortraitUpsideDown = false; //不允许自动旋转到纵向上下 ScreenOrientationHelper.ApplyPortraitForBoot(); #if UNITY_EDITOR StartCoroutine(ScreenOrientationHelper.SwitchToPortraitEditorAndWait()); #endif Screen.sleepTimeout = SleepTimeout.NeverSleep; //睡眠时间为从不睡眠 //游戏帧率设置,需要在项目设置的Quality中关闭对应画质的垂直同步(VSync选项) QualitySettings.vSyncCount = 0; Application.targetFrameRate = 60; //QualitySettings.vSyncCount = 1; // SetTip("Loading", Color.white, 56); //SetTip("正在检测对比软件版本", Color.white); StartCoroutine(CheckAppVersion()); StartCoroutine(AsyncLoadScene()); SceneManager.sceneUnloaded += (scene) => { long t1 = JCUnityLib.TimeUtils.GetTimestamp(); Resources.UnloadUnusedAssets(); System.GC.Collect(); long t2 = JCUnityLib.TimeUtils.GetTimestamp(); Debug.Log($"场景{scene.name}销毁,释放资源和GC回收的总耗时={t2 - t1}ms"); }; PersistenHandler.Init(); SimulateMouseController.Init(); } bool appVersionCheckOK = false; IEnumerator CheckAppVersion() { // countStr1 += "a"; // string url = CommonConfig.businessServerURI + "/app/checkAppVersion?appVersion=" + appVersion; // countStr1 += "b"; // using (UnityWebRequest request = UnityWebRequest.Get(url)) { // countStr1 += "c"; // yield return request.SendWebRequest(); // countStr1 += "d"; // if (request.result == UnityWebRequest.Result.Success) { // countStr1 += "e"; // appVersionCheckOK = bool.Parse(request.downloadHandler.text); // countStr1 += "f"; // if (appVersionCheckOK) { // countStr1 += "g"; // //等场景加载完再提示 // } else { // countStr1 += "h"; // SetTip("请安装最新版本软件", Color.yellow); // } // } else { // countStr1 += "i"; // SetTip("读取服务端软件版本配置失败", Color.red); // } // countStr1 += "j"; // } // countStr1 += "k"; // Debug.Log("countStr1:" + countStr1); yield return null; appVersionCheckOK = true; } float timeOnStartLoadScene; /// 冷启动有本地 Token 时先 HTTP 校验,避免无效 Token 先进 Home。 IEnumerator ValidateStoredLoginToken(Action onDone) { RequestResult res = null; // Entry 场景名服务端不认,须传目标场景 Home(与旧版进 Home 后再 LoginByToken 一致) yield return LoginController.Instance.LoginByToken(r => res = r, "Home"); if (res.code == 0) { string loginToken = (string)res.data; CommonConfig.businessServerWsURL = loginToken.Split('&')[2]; PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken); onDone(true, LoginMgr.BootLoginTipKind.None); } else if (res.code == -9999) { onDone(false, LoginMgr.BootLoginTipKind.NetworkError); } else { Debug.LogWarning($"[Entry] LoginByToken failed code={res.code} msg={res.msg}"); PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey); CommonConfig.businessServerWsURL = null; onDone(false, LoginMgr.BootLoginTipKind.AuthExpired); } } IEnumerator AsyncLoadScene() { countStr2 += "a"; timeOnStartLoadScene = Time.realtimeSinceStartup; //加载场景名 string sceneName = "Login"; if (CommonConfig.StandaloneMode) { sceneName = "Home"; } else if (LoginMgr.HasToken()) { bool tokenValid = false; LoginMgr.BootLoginTipKind failTip = LoginMgr.BootLoginTipKind.None; yield return ValidateStoredLoginToken((ok, tip) => { tokenValid = ok; failTip = tip; }); if (tokenValid) sceneName = "Home"; else { sceneName = "Login"; LoginMgr.PendingBootLoginTip = failTip; } } countStr2 += sceneName; //异步加载场景 AsyncOperation async = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single); countStr2 += "b"; //阻止当加载完成自动切换 async.allowSceneActivation = false; countStr2 += "c"; while (!async.isDone) { if (async.progress >= 0.9f) { countStr2 += "e"; break; } if (!countStr2.Contains("d")) countStr2 += "d"; yield return null; } countStr2 += "f"; while (!appVersionCheckOK) { if (!countStr2.Contains("@")) countStr2 += "@"; yield return null; } countStr2 += "g"; while (Time.realtimeSinceStartup - timeOnStartLoadScene < 1.5) { if (!countStr2.Contains("%")) countStr2 += "%"; yield return null; } countStr2 += "h"; if (sceneName == "Home") yield return ScreenOrientationHelper.SwitchToLandscapeAndWait(); if (sceneName == "Home") ScreenOrientationHelper.ScheduleFadeOutBlackOverlayAfterSceneLoad("Home"); async.allowSceneActivation = true; countStr2 += "i"; Debug.Log("countStr2:" + countStr2); //SetTip("软件版本校验成功", Color.green); } [SerializeField] Text tipText; void SetTip(string text, Color color, int fontSize = 40) { tipText.text = text; tipText.color = color; tipText.fontSize = fontSize; } int counter = 0; string countStr1 = ""; string countStr2 = ""; void OnGUI() { if (CommonConfig.ReleaseVersion2) return; GUIStyle labelFont = new GUIStyle(); labelFont.normal.textColor = new Color(1, 0.6f, 0.6f); labelFont.fontSize = 40; GUI.Label(new Rect(Screen.width/10,Screen.height/10,200,200), (counter++) + "," + countStr1 + "," + countStr2, labelFont); } }