现在,似乎 UnityWebRequest.GetAssetBundle 将资产下载到RAM并加载 . 无论如何要下载到硬盘加载?
UnityWebRequest.GetAssetBundle
这并不复杂 . 处理它就像你从互联网上下载的普通文件,但保存".unity3d"扩展名 .
1 . 通过 UnityWebRequest 发出请求,将 AssetBundle 下载为普通文件 .
UnityWebRequest
AssetBundle
UnityWebRequest www = UnityWebRequest.Get(url); yield return www.Send();
2 . 使用 DownloadHandler.data 检索字节数组数据,然后将其保存到 Application.persistentDataPath/yourfolder/filename.unity3d . 确保扩展名为".unity3d" .
DownloadHandler.data
Application.persistentDataPath/yourfolder/filename.unity3d
File.WriteAllBytes(handle.data, data);
而已 .
3 . 要加载数据,请使用 AssetBundle.LoadFromFile 或 AssetBundle.LoadFromFileAsync :
AssetBundle.LoadFromFile
AssetBundle.LoadFromFileAsync
AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path);
如果仍然困惑,这就是应该是什么样子 . 您可能需要进行一些修改:
Download and Save:
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); } }
Load :
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<GameObject>("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); }
1 回答
这并不复杂 . 处理它就像你从互联网上下载的普通文件,但保存".unity3d"扩展名 .
1 . 通过
UnityWebRequest
发出请求,将AssetBundle
下载为普通文件 .2 . 使用
DownloadHandler.data
检索字节数组数据,然后将其保存到Application.persistentDataPath/yourfolder/filename.unity3d
. 确保扩展名为".unity3d" .而已 .
3 . 要加载数据,请使用
AssetBundle.LoadFromFile
或AssetBundle.LoadFromFileAsync
:如果仍然困惑,这就是应该是什么样子 . 您可能需要进行一些修改:
Download and Save:
Load :