Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信小游戏适配 #267

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected void CheckTimeout()
// 注意:在连续时间段内无新增下载数据及判定为超时
if (_isAbort == false)
{
if (_latestDownloadBytes != DownloadedBytes)
if (_latestDownloadBytes != DownloadedBytes || _latestDownloadRealtime == 0)
{
_latestDownloadBytes = DownloadedBytes;
_latestDownloadRealtime = Time.realtimeSinceStartup;
Expand All @@ -172,6 +172,7 @@ protected void CheckTimeout()
if (offset > _timeout)
{
YooLogger.Warning($"Web file request timeout : {_requestURL}");
_latestDownloadRealtime = 0;
if (_requester != null)
_requester.Abort();
_isAbort = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Net;
using UnityEngine;
using UnityEngine.Networking;

Expand Down Expand Up @@ -29,6 +30,12 @@ public void Create(string requestURL, BundleInfo bundleInfo, params object[] arg
// 解析附加参数
_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);
if (CacheHelper.DisableUnityCacheOnWebGL)
Expand All @@ -47,6 +54,8 @@ public void Create(string requestURL, BundleInfo bundleInfo, params object[] arg
#endif
_webRequest.downloadHandler = _downloadhandler;
_webRequest.disposeDownloadHandlerOnDispose = true;
#endif

_webRequest.SendWebRequest();
Status = ERequestStatus.InProgress;
}
Expand Down Expand Up @@ -92,7 +101,11 @@ public void Update()
{
if (_getAssetBundle)
{
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
_cacheAssetBundle = (_webRequest.downloadHandler as WeChatWASM.DownloadHandlerWXAssetBundle).assetBundle;
#else
_cacheAssetBundle = _downloadhandler.assetBundle;
#endif
if (_cacheAssetBundle == null)
{
RequestNetError = "assetBundle is null";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ public virtual void Destroy()

if (CacheBundle != null)
{
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
WeChatWASM.AssetBundleExtensions.WXUnload(CacheBundle, true);
#else
CacheBundle.Unload(true);
#endif
CacheBundle = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ private List<BundleInfo> ConvertToDownloadList(List<PackageBundle> downloadList)
}

// 查询相关
#if UNITY_WECHAT_GAME
// 未命中缓存的情况,微信小程序会抛出报错,日志数量多了会卡。
#if UNITY_WECHAT_GAME && !UNITY_EDITOR
private WeChatWASM.WXFileSystemManager _wxFileSystemMgr;
private bool IsCachedPackageBundle(PackageBundle packageBundle)
{
if (_wxFileSystemMgr == null)
_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);
return result.Equals("access:ok");
}
Expand Down