From db8d09d0d69402c42eb6efa3c53dce91156aff9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Thu, 4 Jul 2024 22:56:45 +0800 Subject: [PATCH] update extension sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加微信小游戏文件系统范例 --- .../Scripts/Runtime/WechatFileSystem.cs | 135 ++++++++++++++++++ .../Scripts/Runtime/WechatFileSystem.cs.meta | 11 ++ .../Runtime/YooAsset.RuntimeExtension.asmdef | 3 +- 3 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs create mode 100644 Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs.meta diff --git a/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs new file mode 100644 index 0000000..bb1c2bc --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs @@ -0,0 +1,135 @@ +#if UNITY_WECHAT_GAME +using UnityEngine; +using UnityEngine.Networking; +using YooAsset; +using WeChatWASM; + +/// +/// ΢СϷļϵͳչ +/// οhttps://wechat-miniprogram.github.io/minigame-unity-webgl-transform/Design/UsingAssetBundle.html +/// +internal class WechatFileSystem : DefaultWebFileSystem +{ + /// + /// Դļ + /// + public override FSLoadBundleOperation LoadBundleFile(PackageBundle bundle) + { + var operation = new WechatLoadBundleOperation(this, bundle); + OperationSystem.StartOperation(PackageName, operation); + return operation; + } + + /// + /// Դļж + /// + public override void UnloadBundleFile(PackageBundle bundle, object result) + { + AssetBundle assetBundle = result as AssetBundle; + if (assetBundle != null) + assetBundle.WXUnload(true); + } + + + /// + /// дԴļ + /// + internal class WechatLoadBundleOperation : FSLoadBundleOperation + { + private enum ESteps + { + None, + LoadBundleFile, + Done, + } + + private readonly WechatFileSystem _fileSystem; + private readonly PackageBundle _bundle; + private UnityWebRequest _webRequest; + private ESteps _steps = ESteps.None; + + + internal WechatLoadBundleOperation(WechatFileSystem fileSystem, PackageBundle bundle) + { + _fileSystem = fileSystem; + _bundle = bundle; + } + internal override void InternalOnStart() + { + _steps = ESteps.LoadBundleFile; + } + internal override void InternalOnUpdate() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + if (_steps == ESteps.LoadBundleFile) + { + if (_webRequest == null) + { + string mainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); + _webRequest = WXAssetBundle.GetAssetBundle(mainURL); + _webRequest.SendWebRequest(); + } + + DownloadProgress = _webRequest.downloadProgress; + DownloadedBytes = (long)_webRequest.downloadedBytes; + Progress = DownloadProgress; + if (_webRequest.isDone == false) + return; + + if (CheckRequestResult()) + { + _steps = ESteps.Done; + Result = (_webRequest.downloadHandler as DownloadHandlerWXAssetBundle).assetBundle; + Status = EOperationStatus.Succeed; + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + } + } + } + + public override void WaitForAsyncComplete() + { + if (IsDone == false) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "WebGL platform not support sync load method !"; + UnityEngine.Debug.LogError(Error); + } + } + public override void AbortDownloadOperation() + { + } + + private bool CheckRequestResult() + { +#if UNITY_2020_3_OR_NEWER + if (_webRequest.result != UnityWebRequest.Result.Success) + { + Error = _webRequest.error; + return false; + } + else + { + return true; + } +#else + if (_webRequest.isNetworkError || _webRequest.isHttpError) + { + Error = _webRequest.error; + return false; + } + else + { + return true; + } +#endif + } + } +} +#endif \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs.meta b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs.meta new file mode 100644 index 0000000..abc313f --- /dev/null +++ b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/WechatFileSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 97f01c24ae799f34393a4362f33a0e1f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/YooAsset.RuntimeExtension.asmdef b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/YooAsset.RuntimeExtension.asmdef index ad3d122..1440681 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/YooAsset.RuntimeExtension.asmdef +++ b/Assets/YooAsset/Samples~/Extension Sample/Scripts/Runtime/YooAsset.RuntimeExtension.asmdef @@ -2,7 +2,8 @@ "name": "YooAsset.RuntimeExtension", "rootNamespace": "", "references": [ - "GUID:e34a5702dd353724aa315fb8011f08c3" + "GUID:e34a5702dd353724aa315fb8011f08c3", + "GUID:5efd170ecd8084500bed5692932fe14e" ], "includePlatforms": [], "excludePlatforms": [],