2017-07-20 41 views

回答

1

这并不复杂。像处理普通文件一样处理它,你可以从网上下载文件,但用“.unity3d”扩展名保存。

。下载AssetBundle作为正常文件通过UnityWebRequest发出请求。

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

.Retrieve与DownloadHandler.data字节数组数据,然后将其保存到Application.persistentDataPath/yourfolder/filename.unity3d。确保扩展名是“.unity3d”

File.WriteAllBytes(handle.data, data); 

就是这样。

。要加载数据,使用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); 
} 
+0

感谢,如果我要在移动设备上运行,你在哪里推荐储藏? persistentDataPath?如果我想替换旧版本的assetbundle,例如,我拥有assetbundle1_01,然后检测到更新并下载assetbundle1_02,如果用户不给我写入权限,那么这也会有问题吗? – ywj7931

+0

是的,使用persistentDataPath。请注意,您必须**在其中创建一个文件夹并存储在该文件夹中。不要直接保存到persistentDataPath路径。它应该是'Application.persistentDataPath/yourfolder/filename.ext'。您可以随时用新版本替换文件。最后,在保存功能中有一个'try'和'catch'代码。如果有异常,您可以在catch块中检查它并要求用户启用写入权限。如果他们不想要你,你不能写信给别人的设备。 – Programmer

+0

如果您在Android上遇到问题,请务必将“写入权限”更改为“外部(SDCard)” – Programmer

相关问题