From 8d1f0d20104ec832e95dc1e69e84d36b6a7b83e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Wed, 25 Dec 2024 17:24:48 +0800 Subject: [PATCH] update extension sample --- .../ByteGameFileSystem/ByteGameFileSystem.cs | 32 +++++---- .../Operation/BGFSLoadBundleOperation.cs | 17 ++++- .../WechatFileSystem/BundleResult.meta | 8 +++ .../BundleResult/WXAssetBundleResult.cs | 68 +++++++++++++++++++ .../BundleResult/WXAssetBundleResult.cs.meta | 11 +++ .../Operation/WXFSLoadBundleOperation.cs | 18 +++-- .../WechatFileSystem/WechatFileSystem.cs | 36 ++++++---- 7 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult.meta create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs.meta diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/ByteGameFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/ByteGameFileSystem.cs index 1b87d54e..50af4348 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/ByteGameFileSystem.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/ByteGameFileSystem.cs @@ -126,15 +126,19 @@ internal class ByteGameFileSystem : IFileSystem } public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle) { - var operation = new BGFSLoadBundleOperation(this, bundle); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - public virtual void UnloadBundleFile(PackageBundle bundle, object result) - { - AssetBundle assetBundle = result as AssetBundle; - if (assetBundle != null) - assetBundle.Unload(true); + if (bundle.BundleType == (int)EBuildBundleType.AssetBundle) + { + var operation = new BGFSLoadBundleOperation(this, bundle); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + else + { + string error = $"{nameof(ByteGameFileSystem)} not support load bundle type : {bundle.BundleType}"; + var operation = new FSLoadBundleCompleteOperation(error); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } } public virtual void SetParameter(string name, object value) @@ -190,15 +194,19 @@ internal class ByteGameFileSystem : IFileSystem return false; } - public virtual byte[] ReadFileData(PackageBundle bundle) + public virtual string GetBundleFilePath(PackageBundle bundle) { throw new System.NotImplementedException(); } - public virtual string ReadFileText(PackageBundle bundle) + public virtual byte[] ReadBundleFileData(PackageBundle bundle) { throw new System.NotImplementedException(); } - + public virtual string ReadBundleFileText(PackageBundle bundle) + { + throw new System.NotImplementedException(); + } + #region 内部方法 private string GetCacheFileLoadPath(PackageBundle bundle) { diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs index b3a5b5f7..8e671df7 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/ByteGameFileSystem/Operation/BGFSLoadBundleOperation.cs @@ -1,6 +1,7 @@ #if UNITY_WEBGL && BYTEMINIGAME using UnityEngine; using UnityEngine.Networking; +using WeChatWASM; using YooAsset; internal class BGFSLoadBundleOperation : FSLoadBundleOperation @@ -48,9 +49,19 @@ internal class BGFSLoadBundleOperation : FSLoadBundleOperation if (CheckRequestResult()) { - _steps = ESteps.Done; - Result = (_webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle; - Status = EOperationStatus.Succeed; + var assetBundle = (_webRequest.downloadHandler as DownloadHandlerAssetBundle).assetBundle; + if (assetBundle == null) + { + _steps = ESteps.Done; + Error = $"{nameof(DownloadHandlerAssetBundle)} loaded asset bundle is null !"; + Status = EOperationStatus.Failed; + } + else + { + _steps = ESteps.Done; + Result = new AssetBundleResult(_fileSystem, _bundle, assetBundle, null); + Status = EOperationStatus.Succeed; + } } else { diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult.meta new file mode 100644 index 00000000..ebc65f80 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b4c0dec5e8fae814eb7ac0299187e8f4 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs new file mode 100644 index 00000000..932b2afd --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs @@ -0,0 +1,68 @@ +#if UNITY_WEBGL && WEIXINMINIGAME +using System.IO; +using UnityEngine; +using UnityEngine.SceneManagement; +using WeChatWASM; + +namespace YooAsset +{ + internal class WXAssetBundleResult : BundleResult + { + private readonly IFileSystem _fileSystem; + private readonly PackageBundle _packageBundle; + private readonly AssetBundle _assetBundle; + + public WXAssetBundleResult(IFileSystem fileSystem, PackageBundle packageBundle, AssetBundle assetBundle) + { + _fileSystem = fileSystem; + _packageBundle = packageBundle; + _assetBundle = assetBundle; + } + + public override void UnloadBundleFile() + { + if (_assetBundle != null) + { + _assetBundle.WXUnload(true); + } + } + public override string GetBundleFilePath() + { + return _fileSystem.GetBundleFilePath(_packageBundle); + } + public override byte[] ReadBundleFileData() + { + return _fileSystem.ReadBundleFileData(_packageBundle); + } + public override string ReadBundleFileText() + { + return _fileSystem.ReadBundleFileText(_packageBundle); + } + + public override FSLoadAssetOperation LoadAssetAsync(AssetInfo assetInfo) + { + var operation = new AssetBundleLoadAssetOperation(_packageBundle, _assetBundle, assetInfo); + OperationSystem.StartOperation(_fileSystem.PackageName, operation); + return operation; + } + public override FSLoadAllAssetsOperation LoadAllAssetsAsync(AssetInfo assetInfo) + { + var operation = new AssetBundleLoadAllAssetsOperation(_packageBundle, _assetBundle, assetInfo); + OperationSystem.StartOperation(_fileSystem.PackageName, operation); + return operation; + } + public override FSLoadSubAssetsOperation LoadSubAssetsAsync(AssetInfo assetInfo) + { + var operation = new AssetBundleLoadSubAssetsOperation(_packageBundle, _assetBundle, assetInfo); + OperationSystem.StartOperation(_fileSystem.PackageName, operation); + return operation; + } + public override FSLoadSceneOperation LoadSceneOperation(AssetInfo assetInfo, LoadSceneParameters loadParams, bool suspendLoad) + { + var operation = new AssetBundleLoadSceneOperation(assetInfo, loadParams, suspendLoad); + OperationSystem.StartOperation(_fileSystem.PackageName, operation); + return operation; + } + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs.meta new file mode 100644 index 00000000..0ec0d546 --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/BundleResult/WXAssetBundleResult.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 750c122b5d921aa4a9c882e21982ebd5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs index 3c2f02a9..d0f241e2 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs @@ -49,9 +49,19 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation if (CheckRequestResult()) { - _steps = ESteps.Done; - Result = (_webRequest.downloadHandler as DownloadHandlerWXAssetBundle).assetBundle; - Status = EOperationStatus.Succeed; + var assetBundle = (_webRequest.downloadHandler as DownloadHandlerWXAssetBundle).assetBundle; + if (assetBundle == null) + { + _steps = ESteps.Done; + Error = $"{nameof(DownloadHandlerWXAssetBundle)} loaded asset bundle is null !"; + Status = EOperationStatus.Failed; + } + else + { + _steps = ESteps.Done; + Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle); + Status = EOperationStatus.Succeed; + } } else { @@ -66,7 +76,7 @@ internal class WXFSLoadBundleOperation : FSLoadBundleOperation { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = "WebGL platform not support sync load method !"; + Error = "Wechat platform not support sync load method !"; UnityEngine.Debug.LogError(Error); } } diff --git a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs index 3722b377..34567373 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Runtime/WechatFileSystem/WechatFileSystem.cs @@ -147,15 +147,19 @@ internal class WechatFileSystem : IFileSystem } public virtual FSLoadBundleOperation LoadBundleFile(PackageBundle bundle) { - var operation = new WXFSLoadBundleOperation(this, bundle); - OperationSystem.StartOperation(PackageName, operation); - return operation; - } - public virtual void UnloadBundleFile(PackageBundle bundle, object result) - { - AssetBundle assetBundle = result as AssetBundle; - if (assetBundle != null) - assetBundle.WXUnload(true); + if (bundle.BundleType == (int)EBuildBundleType.AssetBundle) + { + var operation = new WXFSLoadBundleOperation(this, bundle); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + else + { + string error = $"{nameof(WechatFileSystem)} not support load bundle type : {bundle.BundleType}"; + var operation = new FSLoadBundleCompleteOperation(error); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } } public virtual void SetParameter(string name, object value) @@ -181,9 +185,9 @@ internal class WechatFileSystem : IFileSystem } _wxFileSystemMgr = WX.GetFileSystemManager(); - _fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; - //_fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE/子目录"; //注意:如果有子目录,请修改此处! - //_fileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL"); + _fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; + //_fileCacheRoot = $"{WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE/子目录"; //注意:如果有子目录,请修改此处! + //_fileCacheRoot = PathUtility.Combine(WX.PluginCachePath, $"StreamingAssets/WebGL"); } public virtual void OnUpdate() { @@ -214,7 +218,11 @@ internal class WechatFileSystem : IFileSystem return false; } - public virtual byte[] ReadFileData(PackageBundle bundle) + public virtual string GetBundleFilePath(PackageBundle bundle) + { + return GetCacheFileLoadPath(bundle); + } + public virtual byte[] ReadBundleFileData(PackageBundle bundle) { string filePath = GetCacheFileLoadPath(bundle); if (CheckCacheFileExist(filePath)) @@ -222,7 +230,7 @@ internal class WechatFileSystem : IFileSystem else return Array.Empty(); } - public virtual string ReadFileText(PackageBundle bundle) + public virtual string ReadBundleFileText(PackageBundle bundle) { string filePath = GetCacheFileLoadPath(bundle); if (CheckCacheFileExist(filePath))