gpt4 book ai didi

c# - 下载硬盘中的AssetBundle

转载 作者:太空狗 更新时间:2023-10-29 22:57:02 38 4
gpt4 key购买 nike

现在,似乎UnityWebRequest.GetAssetBundle将 Assets 下载到 RAM 并加载。反正有下载到硬盘加载吗?

最佳答案

这并不复杂。像处理从 Internet 下载的普通文件一样处理它,但使用 ".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.LoadFromFileAssetBundle.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<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);
}

关于c# - 下载硬盘中的AssetBundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45226800/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com