在硬盘中下载AssetBundle
现在,似乎UnityWebRequest.GetAssetBundle
将资产下载到RAM并加载。 无论如何要下载到硬盘加载?
这并不复杂。 处理它就像您从互联网上下载的普通文件,但保存为“.unity3d”扩展名。
1.使用 UnityWebRequest发出请求,将AssetBundle
为普通文件。
UnityWebRequest www = UnityWebRequest.Get(url); yield return www.Send();
2.使用DownloadHandler.data
检索字节数组数据,然后将其保存到Application.persistentDataPath/yourfolder/filename.unity3d
。 确保扩展名为“.unity3d” 。
File.WriteAllBytes(handle.data, data);
而已。
3.要加载数据,请使用AssetBundle.LoadFromFile
或AssetBundle.LoadFromFileAsync
:
AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path);
如果仍然困惑,这就是应该是什么样子。 您可能需要进行一些修改:
下载并保存:
IEnumerator downloadAsset() { string url = "http://url.net/YourAsset.unity3d"; UnityWebRequest www = UnityWebRequest.Get(url); DownloadHandler handle = www.downloadHandler; //Send Request and wait yield return www.Send(); if (www.isError) { UnityEngine.Debug.Log("Error while Downloading Data: " + www.error); } else { UnityEngine.Debug.Log("Success"); //handle.data //Construct path to save it string dataFileName = "WaterVehicles"; string tempPath = Path.Combine(Application.persistentDataPath, "AssetData"); tempPath = Path.Combine(tempPath, dataFileName + ".unity3d"); //Save save(handle.data, tempPath); } } void save(byte[] data, string path) { //Create the Directory if it does not exist if (!Directory.Exists(Path.GetDirectoryName(path))) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } try { File.WriteAllBytes(path, data); Debug.Log("Saved Data to: " + path.Replace("/", "\\")); } catch (Exception e) { Debug.LogWarning("Failed To Save Data to: " + path.Replace("/", "\\")); Debug.LogWarning("Error: " + e.Message); } }
装载 :
IEnumerable LoadObject(string path) { AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path); yield return bundle; AssetBundle myLoadedAssetBundle = bundle.assetBundle; if (myLoadedAssetBundle == null) { Debug.Log("Failed to load AssetBundle!"); yield break; } AssetBundleRequest request = myLoadedAssetBundle.LoadAssetAsync("boat"); yield return request; GameObject obj = request.asset as GameObject; obj.transform.position = new Vector3(0.08f, -2.345f, 297.54f); obj.transform.Rotate(350.41f, 400f, 20f); obj.transform.localScale = new Vector3(1.0518f, 0.998f, 1.1793f); Instantiate(obj); myLoadedAssetBundle.Unload(false); }