| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using UnityEngine.SceneManagement;
- /* 程序启动入口 */
- public class Entry : MonoBehaviour
- {
- void Awake()
- {
- #if UNITY_IOS
- if (CommonConfig.iosTaoKe && PlayerPrefs.GetInt("CanGoToApp", 0) == 0)
- {
- Destroy(gameObject);
- SceneManager.LoadScene("HeartRateBand");
- return;
- }
- #endif
- GameObject.Find("Canvas").GetComponent<Canvas>().enabled = true;
- GameObject.Find("CanvasLogo").GetComponent<Canvas>().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.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);
- //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;
- IEnumerator AsyncLoadScene() {
- countStr2 += "a";
- timeOnStartLoadScene = Time.realtimeSinceStartup;
- //加载场景名
- string sceneName = "Login";
- if (LoginMgr.HasToken()) {
- sceneName = "Home";
- }
- 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";
- 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);
- }
- }
|