- Home /
Question by
lijia2010114105 · May 24 at 06:37 PM ·
assetbundle
After downloading the AssetBundle, how do I save it locally?
When I downloaded the AssetBundle and saved it, I ran into a problem at "byte[] results = request1.downloadHandler.data;" An error is as follows: “unity Failed to read data for the AssetBundle”
Here is my code: public IEnumerator GetRequest(string url) { UnityWebRequest request1 = UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(url);
yield return request1.SendWebRequest();
if (request1.isDone)
{
if (request1.result != UnityWebRequest.Result.Success)
{
Debug.Log(request1.error);
}
else
{
AssetBundle ab = UnityEngine.Networking.DownloadHandlerAssetBundle.GetContent(request1);
//加载AB包
loadKeshengAB(ab);
byte[] results = request1.downloadHandler.data;
Debug.Log(results.Length);
saveABToLocal(results);
}
}
}
void saveABToLocal(byte[] results) {
//byte[] results = request1.downloadHandler.data;
FileInfo fileInfo = new FileInfo(localPath + "/" + filename);
FileStream fs = fileInfo.Create();
//fs.Write(字节数组, 开始位置, 数据长度);
fs.Write(results, 0, results.Length);
fs.Flush(); //文件写入存储到硬盘
fs.Close(); //关闭文件流对象
fs.Dispose(); //销毁文件对象
}
Comment
Your answer
Follow this Question
Related Questions
How to import the object from server to unity 2 Answers
Ios assetbundles are null 2 Answers
Assign www.assetBundle to new AssetBundle Freezes WebPlayer 0 Answers
AssetBundles coding 1 Answer
Load Scene from Asset Bundle? 0 Answers