using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.IO; public class LaunchInHot : SingletonMonobehaviour { static int step = 0; public static LaunchStage m_CurrentStage = LaunchStage.None; public static LaunchProgressInfo progressInfo { get; private set; } float timer = 0f; Queue tasks = new Queue(); LaunchTask currentTask = null; bool launchComplete = false; float surplusProgress = 0f; float surplusTime = 0f; public Action OnApplicationOut = null; void Start() { System.Net.ServicePointManager.DefaultConnectionLimit = 100;//设置http最大连接数 Application.backgroundLoadingPriority = ThreadPriority.High; Screen.sleepTimeout = SleepTimeout.NeverSleep; SDKUtils.Instance.Init(); //原sdk接口 if (!AssetSource.isUseAssetBundle) { InitSystemMgr(); // 这里还没开始加载配置 但是提前加载了供LaunchWin使用 PriorLanguageConfig.LazyInit(); InitialFunctionConfig.LazyInit(); UIManager.Instance.OpenWindow(); } var builtInAssetCopyTask = new BuiltInAssetCopyTask(); var requestPermissionStart = new RequestPermissionStart(); var initSettingTask = new InitSettingTask(); var sdkInitedTask = new SDKInitedTask(); //var assetCopyTask = new AssetCopyTask(); //var assetDecompressTask = new AssetDecompressTask(); var getVersionInfoTask = new GetVersionInfoTask(); var checkAssetValidTask = new CheckAssetValidTask(); var downLoadAssetTask = new DownLoadAssetTask(); var assetBundleInitTask = new AssetBundleInitTask(); var configInitTask = new ConfigInitTask(); var launchFadeOutTask = new LaunchFadeOutTask(); tasks.Enqueue(builtInAssetCopyTask); tasks.Enqueue(requestPermissionStart); tasks.Enqueue(initSettingTask); if (!Application.isEditor) { tasks.Enqueue(sdkInitedTask); } switch (Application.platform) { case RuntimePlatform.OSXEditor: case RuntimePlatform.WindowsEditor: tasks.Enqueue(getVersionInfoTask); break; case RuntimePlatform.Android: tasks.Enqueue(getVersionInfoTask); //tasks.Enqueue(assetCopyTask); //tasks.Enqueue(assetDecompressTask); break; case RuntimePlatform.IPhonePlayer: tasks.Enqueue(getVersionInfoTask); //tasks.Enqueue(assetCopyTask); break; case RuntimePlatform.WindowsPlayer: //tasks.Enqueue(assetCopyTask); tasks.Enqueue(getVersionInfoTask); break; } tasks.Enqueue(checkAssetValidTask); tasks.Enqueue(downLoadAssetTask); tasks.Enqueue(assetBundleInitTask); tasks.Enqueue(configInitTask); tasks.Enqueue(launchFadeOutTask); CalculateExpectTotalTime(); } public void InitSystemMgr() { ResManager.Instance.Init(); UIManager.Instance.Init(); StageManager.Instance.Init(); LoginManager.Instance.Init(); } void Update() { if (!launchComplete) { if (currentTask == null) { if (tasks.Count > 0) { currentTask = tasks.Dequeue(); currentTask.Begin(); } else { launchComplete = true; } } if (currentTask != null) { currentTask.Update(); } if (currentTask != null && currentTask.done) { currentTask.End(); CalculateExpectTotalTime(); currentTask = null; } if (m_CurrentStage == LaunchStage.DownLoad) { progressInfo = new LaunchProgressInfo(m_CurrentStage, 1, progressInfo.totalProgress, 0f); } else { timer += Time.deltaTime; var progress = progressInfo.totalProgress + surplusProgress * (Time.deltaTime / surplusTime); progress = Mathf.Clamp(progress, 0, 0.98f); var partProgress = 0f; if (currentTask == null) { partProgress = 0f; } else { var temp = currentTask.timer / Mathf.Min(2f, currentTask.duration); step = (int)temp + 1; partProgress = temp - (int)temp; } progressInfo = new LaunchProgressInfo(m_CurrentStage, step, Mathf.Clamp01(progress), partProgress); } } // Debug.LogError("currentTask is null " + (currentTask == null).ToString() + " isLaunchComplete " + launchComplete.ToString()); // if (currentTask != null) // { if (launchComplete) { UnityEngine.Debug.LogFormat("启动耗时:{0}", timer); progressInfo = new LaunchProgressInfo(m_CurrentStage, 1, 1f, 1f); this.enabled = false; Main.Init(); // StageLoad.Instance.PushSceneLoadCommand(new StageLoad.StageLoadCommand() // { // toMapId = 1, // toLineId = 0, // needEmpty = false, // needLoadResource = true, // serverType = ServerType.Main, // isClientLoadMap = true // }); } // } } public static int GetLaunchStage() { return (int)m_CurrentStage; } /// /// 计算总的预期时间 /// void CalculateExpectTotalTime() { surplusTime = 0f; foreach (var item in tasks) { surplusTime += item.expectTime; } surplusProgress = 1 - progressInfo.totalProgress; } private void OnApplicationQuit() { OnApplicationOut?.Invoke(); } }