using Cysharp.Threading.Tasks; using PhxhSDK.AOT.VersionUpdate; using UnityEngine; using YooAsset; namespace PhxhSDK.AOT { public class YooAssetHelper { public static ResourcePackage defaultPackage; public static ResourcePackage rawPackage; public static async UniTask Init() { if (defaultPackage != null) return; // 初始化资源系统 YooAssets.Initialize(); // 创建默认的资源包 defaultPackage = YooAssets.CreatePackage("DefaultPackage"); rawPackage = YooAssets.CreatePackage("RawPackage"); // 设置该资源包为默认的资源包,可以使用YooAssets相关加载接口加载该资源包内容。 YooAssets.SetDefaultPackage(defaultPackage); #if UNITY_EDITOR #if AB_DEBUG await _InitRemote(); #else await _InitEditor(); #endif #else await _InitRemote(); #endif } private static async UniTask _InitEditor() { Debug.Log("[LTRes]开始初始化编辑器模拟模式"); var initParametersDefault = new EditorSimulateModeParameters(); initParametersDefault.AutoDestroyAssetProvider = true; initParametersDefault.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(EDefaultBuildPipeline.BuiltinBuildPipeline, "DefaultPackage"); var op = defaultPackage.InitializeAsync(initParametersDefault); await op.Task; if (op.Status != EOperationStatus.Succeed) { Debug.LogError("[LTRes]初始化编辑器模拟模式失败"); return false; } var initParametersRaw = new EditorSimulateModeParameters(); initParametersRaw.AutoDestroyAssetProvider = true; initParametersRaw.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(EDefaultBuildPipeline.RawFileBuildPipeline, "RawPackage"); var op2 = rawPackage.InitializeAsync(initParametersRaw); await op2.Task; if (op2.Status != EOperationStatus.Succeed) { Debug.LogError("[LTRes]初始化编辑器模拟模式失败"); return false; } return true; } public static async UniTask InitEditor() { await _InitEditor(); } private static async UniTask _InitRemote() { if (!_isInit) { _isInit = true; await _InitPackage(defaultPackage, false); await _InitPackage(rawPackage, true); Debug.Log("[LTRes]InitializeAsync 完成"); } else { Debug.Log("[LTRes]开始重新尝试初始化remote load"); } return true; } private static bool _isInit = false; private static async UniTask _InitPackage(ResourcePackage package, bool noEncrypt) { Debug.Log("开始初始化package:" + package); var initParameters = new HostPlayModeParameters(); initParameters.AutoDestroyAssetProvider = true; initParameters.BuildinQueryServices = new GameQueryServices(); // initParameters.DeliveryQueryServices = new GameDeliveryServices(); if (noEncrypt) { initParameters.DecryptionServices = new YooDecryptionNone(); } else { initParameters.DecryptionServices = new YooDecryptionFS(); } PackDataInst.CreateInst(); var workSpace = PackDataInst.inst.localInfo.workSpace; var version = PackDataInst.inst.localInfo.version; var buildTarget = PackDataInst.GetRuntimePlatform(); var serverUrl = PackDataInst.inst.localInfo.serverUrl; var pathRoot = PackDataInst.inst.localInfo.pathRoot; var hostPath = $"{serverUrl}/{pathRoot}/{workSpace}/{buildTarget}/{version}"; initParameters.RemoteServices = new LTRemoteServices(hostPath); await package.InitializeAsync(initParameters).Task; Debug.Log("初始化package结束:" + package); } } }