微信小游戏适配

1.查询接口更新
2.适配WXAssetBundle
pull/267/head
zensyue 2024-04-02 01:55:26 +08:00
parent 4c619778c3
commit a93704c60b
3 changed files with 21 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using System.IO; using System.IO;
using System.Net;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
@ -29,6 +30,12 @@ namespace YooAsset
// 解析附加参数 // 解析附加参数
_getAssetBundle = (bool)args[0]; _getAssetBundle = (bool)args[0];
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
uint crc = bundleInfo.Bundle.UnityCRC;
_webRequest = WeChatWASM.WXAssetBundle.GetAssetBundle(requestURL, crc);
_webRequest.disposeDownloadHandlerOnDispose = false;
#else
// 创建下载器 // 创建下载器
_webRequest = DownloadHelper.NewRequest(requestURL); _webRequest = DownloadHelper.NewRequest(requestURL);
if (CacheHelper.DisableUnityCacheOnWebGL) if (CacheHelper.DisableUnityCacheOnWebGL)
@ -47,6 +54,8 @@ namespace YooAsset
#endif #endif
_webRequest.downloadHandler = _downloadhandler; _webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true; _webRequest.disposeDownloadHandlerOnDispose = true;
#endif
_webRequest.SendWebRequest(); _webRequest.SendWebRequest();
Status = ERequestStatus.InProgress; Status = ERequestStatus.InProgress;
} }
@ -92,7 +101,11 @@ namespace YooAsset
{ {
if (_getAssetBundle) if (_getAssetBundle)
{ {
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
_cacheAssetBundle = (_webRequest.downloadHandler as WeChatWASM.DownloadHandlerWXAssetBundle).assetBundle;
#else
_cacheAssetBundle = _downloadhandler.assetBundle; _cacheAssetBundle = _downloadhandler.assetBundle;
#endif
if (_cacheAssetBundle == null) if (_cacheAssetBundle == null)
{ {
RequestNetError = "assetBundle is null"; RequestNetError = "assetBundle is null";

View File

@ -156,7 +156,11 @@ namespace YooAsset
if (CacheBundle != null) if (CacheBundle != null)
{ {
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
WeChatWASM.AssetBundleExtensions.WXUnload(CacheBundle, true);
#else
CacheBundle.Unload(true); CacheBundle.Unload(true);
#endif
CacheBundle = null; CacheBundle = null;
} }
} }

View File

@ -65,13 +65,15 @@ namespace YooAsset
} }
// 查询相关 // 查询相关
#if UNITY_WECHAT_GAME // 未命中缓存的情况,微信小程序会抛出报错,日志数量多了会卡。
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
private WeChatWASM.WXFileSystemManager _wxFileSystemMgr; private WeChatWASM.WXFileSystemManager _wxFileSystemMgr;
private bool IsCachedPackageBundle(PackageBundle packageBundle) private bool IsCachedPackageBundle(PackageBundle packageBundle)
{ {
if (_wxFileSystemMgr == null) if (_wxFileSystemMgr == null)
_wxFileSystemMgr = WeChatWASM.WX.GetFileSystemManager(); _wxFileSystemMgr = WeChatWASM.WX.GetFileSystemManager();
string filePath = WeChatWASM.WX.env.USER_DATA_PATH + packageBundle.FileName; // 如果微信小游戏设置的CDN后面还有子目录这里也要相应添加
string filePath = System.IO.Path.Combine(WeChatWASM.WX.env.USER_DATA_PATH, "__GAME_FILE_CACHE", packageBundle.FileName);
string result = _wxFileSystemMgr.AccessSync(filePath); string result = _wxFileSystemMgr.AccessSync(filePath);
return result.Equals("access:ok"); return result.Equals("access:ok");
} }