using UnityEngine; using System.Collections; using Snxxz.UI; using System; using TableConfig; using System.Collections.Generic; using System.IO; public class Launch : MonoBehaviour { static LaunchStage m_CurrentStage = LaunchStage.None; static public LaunchStage currentStage { get { return m_CurrentStage; } } static float m_Progress = 0f; static public float progress { get { return m_Progress; } set { if (m_Progress != value) { m_Progress = value; if (progressEvent != null) { progressEvent(m_CurrentStage, m_Progress); } } } } public static event Action progressEvent; private void Awake() { ResourcesPath.Instance.Init(); SnxxzGame.Instance.gameObject.name = "__SnxxzGame__"; Application.backgroundLoadingPriority = ThreadPriority.BelowNormal; Screen.sleepTimeout = SleepTimeout.NeverSleep; ShaderUtility.InitGlobalParams(); SoundPlayer.CreateSoundPlayer(); SoundPlayer.Instance.PlayLoginMusic(); SystemSetting.Instance.SetSoundVolume(SystemSetting.Instance.GetSoundVolume()); SystemSetting.Instance.SetGameFps(SystemSetting.Instance.GetGameFps()); SDKUtility.Instance.Init(); GameObjectPoolManager.Instance.gameObject.name = "GameObjectPool"; GameObjectPoolManager.Instance.Initialize(); ExceptionCatcher.Init(); ExceptionCatcher.Catch(); DebugUtility.Instance.Init(); GlobalTimeEvent.Instance.Begin(); } void Start() { Config.Instance.PreLoadConfigs(); ServerListCenter.Instance.RequestJumpUrl(); WindowCenter.Instance.OpenFromLocal(); DebugUtility.Instance.CreateDebugRoot(); StartCoroutine(Co_Lanuch()); } IEnumerator Co_Lanuch() { #if !UNITY_EDITOR while (!SDKUtility.Instance.InitFinished) { yield return null; } #endif var cpu = 2; var memory = 2048; DeviceUtility.GetCpuAndMemory(out cpu, out memory); DebugEx.LogFormat("获得机器信息:cpu {0}----内存 {1}", cpu, memory); var startTime = Time.time; OperationLogCollect.Instance.RecordLauchEvent(1); OperationLogCollect.Instance.RecordEvent(1); SystemSetting.Instance.LetFPSUnLimit(); Config.Instance.RegisterGlobalEvent(); var progressBuf = progress; var timer = 0f; var duration = 1f; PackageRegedit.Init(); #if UNITY_ANDROID && !UNITY_EDITOR switch (VersionConfig.Get().assetAccess) { case InstalledAsset.FullAsset: case InstalledAsset.HalfAsset: case InstalledAsset.IngoreDownLoad: progressBuf = progress; timer = 0f; duration = 100f; if (!SDKUtility.Instance.AssetCopyFinished) { m_CurrentStage = LaunchStage.AssetCopy; SDKUtility.Instance.CopyAsset(); while (!SDKUtility.Instance.AssetCopyFinished) { timer += Time.deltaTime; progress = Mathf.Clamp(progressBuf + timer / duration * 0.3f, progressBuf, progressBuf + 0.3f); yield return null; } } break; case InstalledAsset.NullAsset: progress = 0.1f; break; } #endif #if UNITY_IOS && !UNITY_EDITOR switch (VersionConfig.Get().assetAccess) { case InstalledAsset.FullAsset: case InstalledAsset.HalfAsset: case InstalledAsset.IngoreDownLoad: if (!SDKUtility.Instance.AssetCopyFinished) { m_CurrentStage = LaunchStage.AssetCopy; progressBuf = progress; var allFiles = new List(); FileExtersion.GetAllDirectoryFileInfos(ResourcesPath.Instance.StreamingAssetPath, allFiles); var count = allFiles.Count; var index = 0; while (index < count) { try { var fileInfo = allFiles[index]; var destPath = fileInfo.FullName.Replace(ResourcesPath.Instance.StreamingAssetPath, ResourcesPath.Instance.ExternalStorePath); if (File.Exists(destPath)) { index++; continue; } var destDirectoryName = Path.GetDirectoryName(destPath); if (!Directory.Exists(destDirectoryName)) { Directory.CreateDirectory(destDirectoryName); } DebugEx.LogFormat("拷贝文件:{0}", fileInfo.Name); File.Copy(fileInfo.FullName, destPath, true); index++; } catch (Exception ex) { DebugEx.Log(ex); } finally { progress = Mathf.Clamp(progressBuf + ((float)index / count) * 0.3f, progressBuf, progressBuf + 0.3f); } yield return null; } LocalSave.SetString("AssetCopyCompleted_IOS", VersionConfig.Get().version); } break; case InstalledAsset.NullAsset: progress = 0.1f; break; } #endif OperationLogCollect.Instance.RecordLauchEvent(2); OperationLogCollect.Instance.RecordEvent(2); m_CurrentStage = LaunchStage.ClientVersion; if (!Application.isEditor || InGameDownTestUtility.enable) { VersionUtility.Instance.RequestVersionCheck(); progressBuf = progress; timer = 0f; duration = 1f; while (!VersionUtility.Instance.completed) { timer += Time.deltaTime; progress = Mathf.Clamp(progressBuf + timer / duration * 0.1f, progressBuf, progressBuf + 0.1f); yield return null; } } m_CurrentStage = LaunchStage.DownLoad; switch (VersionConfig.Get().assetAccess) { case InstalledAsset.IngoreDownLoad: break; case InstalledAsset.NullAsset: case InstalledAsset.HalfAsset: case InstalledAsset.FullAsset: AssetVersionUtility.GetAssetVersionFile(); progressBuf = progress; timer = 0f; duration = 1.5f; while (!AssetVersionUtility.checkAssetCompleted) { timer += Time.deltaTime; progress = Mathf.Clamp(progressBuf + timer / duration * 0.3f, progressBuf, progressBuf + 0.3f); yield return null; } if (!AssetVersionUtility.priorAssetDownLoadDone) { AssetVersionUtility.BeginDownLoadTask(true); while (!AssetVersionUtility.priorAssetDownLoadDone) { yield return null; } yield return WaitingForSecondConst.WaitMS200; } if (!AssetVersionUtility.unPriorAssetDownLoadDone) { AssetVersionUtility.BeginDownLoadTask(false); } break; } if (!AssetSource.allFromEditor) { StartCoroutine(AssetBundleUtility.Instance.Initialize()); } m_CurrentStage = LaunchStage.ConfigLoad; LaunchPostProcess.Instance.Begin(); progressBuf = progress; while (!LaunchPostProcess.Instance.completed && LaunchPostProcess.Instance.progress < (Application.platform == RuntimePlatform.WindowsEditor ? 1f : 0.8f)) { progress = Mathf.Clamp(progressBuf + LaunchPostProcess.Instance.progress * (1 - progressBuf), progressBuf, 1f); yield return null; } OperationLogCollect.Instance.RecordLauchEvent(3); OperationLogCollect.Instance.RecordEvent(3); ShaderUtility.WarmUpAll(); SpeechTranslate.Instance.RequestGetToken(); if (!AssetSource.allFromEditor) { progressBuf = progress; timer = 0f; duration = 3f; while (!AssetBundleUtility.Instance.initialized) { timer += Time.deltaTime; progress = Mathf.Clamp(progressBuf + timer / duration * 0.2f, progressBuf, 1f); yield return null; } AssetBundleUtility.Instance.Sync_LoadAll("ui/prioritywindow"); SnxxzGame.Instance.StartCoroutine(AssetBundleUtility.Instance.InitalizeUIResources()); } UI3DModelExhibition.CreateStage(); var launchWin = WindowCenter.Instance.Get(); if (launchWin != null) { launchWin.FadeOut(); } WindowCenter.Instance.Open(true); var launchBackGroundWin = WindowCenter.Instance.Get(); launchBackGroundWin.transform.SetAsFirstSibling(); progressBuf = progress; timer = 0f; duration = 1f; while (timer < duration) { timer += Time.deltaTime; progress = Mathf.Clamp(progressBuf + timer / duration, progressBuf, 1f); yield return null; } StageManager.Instance.LoadLoginStage(); Debug.LogFormat("启动耗时:{0}", Time.time - startTime); } public enum LaunchStage { None, AssetCopy, ClientVersion, DownLoad, ConfigLoad, Complete, } }