小程序
小游戏
企业微信
微信支付
扫描小程序码分享
使用的是AssetBundle.LoadFromFIle这个接口,在unity编辑器上使用的情况下是没有问题,但微信小游戏上会出现Unable to open archive file
还有一个问题是下载的assetbundle包无法写入到Application.persistentDataPath里面。
求请教下这是怎么回事
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
UnityWebRequest req = new UnityWebRequest(path); DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(req.url, 0); req.downloadHandler = handler; yield return req.SendWebRequest(); AssetBundleab = handler.assetBundle;
使用上面的代码下载AssetBundle会报出同样的错误,Unable to open archive file: path。这个path点击后是可以正确下载资源。(将path粘贴到浏览器地址栏)
在Editor下可以完美运行,但是打包到小游戏就会报错了,有什么解法么?
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
微信小游戏提供了一个名为 wx.env.USER_DATA_PATH 的变量,表示用户的可读写路径。您可以使用该路径来存储和访问下载的 AssetBundle 包。
wx.env.USER_DATA_PATH
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
UnityWebRequest req = new UnityWebRequest(path); DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(req.url, 0); req.downloadHandler = handler; yield return req.SendWebRequest(); AssetBundleab = handler.assetBundle;
使用上面的代码下载AssetBundle会报出同样的错误,Unable to open archive file: path。这个path点击后是可以正确下载资源。(将path粘贴到浏览器地址栏)
在Editor下可以完美运行,但是打包到小游戏就会报错了,有什么解法么?
微信小游戏提供了一个名为
wx.env.USER_DATA_PATH
的变量,表示用户的可读写路径。您可以使用该路径来存储和访问下载的 AssetBundle 包。using UnityEngine;
using UnityEngine.Networking;
public class DownloadAssetBundle : MonoBehaviour
{
private string assetBundleURL = "http://example.com/assetbundle.unity3d";
private string savePath;
void Start()
{
// 构造要保存的文件路径
savePath = $"{wx.env.USER_DATA_PATH}/your_assetbundle_name.unity3d";
// 开始下载 AssetBundle
StartCoroutine(DownloadAndSaveAssetBundle());
}
IEnumerator DownloadAndSaveAssetBundle()
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(assetBundleURL))
{
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success)
{
// 下载成功,将数据保存到文件
System.IO.File.WriteAllBytes(savePath, webRequest.downloadHandler.data);
Debug.Log("AssetBundle 下载并保存成功!");
}
else
{
// 下载失败,打印错误信息
Debug.LogError($"AssetBundle 下载失败:{webRequest.error}");
}
}
}
}
在上述示例代码中,assetBundleURL 是要下载的 AssetBundle 的远程 URL,您需要将其替换为实际的 URL。savePath 是您希望将 AssetBundle 保存到的本地路径,通过拼接 wx.env.USER_DATA_PATH 和文件名来构造完整的路径。
在下载成功后,使用 System.IO.File.WriteAllBytes() 方法将二进制数据写入到指定的文件路径中,从而实现将 AssetBundle 保存到指定地址的操作。