本文介绍了在硬盘中下载 AssetBundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

现在,似乎UnityWebRequest.GetAssetBundle将资产下载到RAM并加载.有没有办法下载到硬盘加载?

Right now, it seems UnityWebRequest.GetAssetBundledownload asset to RAM and load. Is there anyway to download to hard disk to load?

推荐答案

这并不复杂.像处理从 Internet 下载的普通文件一样处理它,但使用 ".unity3d" 扩展名保存它.

This is not really complicated. Handle it like a normal file you would download from the internet but save it with the ".unity3d" extension.

1.通过使用 UnityWebRequest 发出请求,将 AssetBundle 作为普通文件下载.

1.Download the AssetBundle as a normal file by making a request with UnityWebRequest.

UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.Send();

2.使用DownloadHandler.data 检索字节数组数据,然后将其保存到Application.persistentDataPath/yourfolder/filename.unity3d.确保扩展名为 ".unity3d".

2.Retrieve the byte array data with DownloadHandler.data then save it to Application.persistentDataPath/yourfolder/filename.unity3d. Make sure that the extension is ".unity3d".

File.WriteAllBytes(handle.data, data);

就是这样.

3.要加载数据,请使用 AssetBundle.LoadFromFileAssetBundle.LoadFromFileAsync:

3.To load the data, use AssetBundle.LoadFromFile or AssetBundle.LoadFromFileAsync:

AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path);

如果仍然感到困惑,这就是事情的样子.您可能需要进行一些修改:


If still confused, here is what the thing should look like. You may need to make some modification:

下载并保存:

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<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);
}

这篇关于在硬盘中下载 AssetBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:04