2017-07-20 8 views

答えて

1

これは実際には複雑ではありません。インターネットからダウンロードする普通のファイルのように扱いますが、".unity3d"拡張子で保存してください。

.Download AssetBundleUnityWebRequestで要求を行うことにより、通常のファイルとして。 DownloadHandler.data

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

.Retrieveバイト配列データは、その後Application.persistentDataPath/yourfolder/filename.unity3dに保存します。拡張子が".unity3d"であることを確認してください。

File.WriteAllBytes(handle.data, data); 

これだけです。

データをロード.TO、AssetBundle.LoadFromFileまたはAssetBundle.LoadFromFileAsyncを使用します。まだ混乱している場合

AssetBundleCreateRequest bundle = AssetBundle.LoadFromFileAsync(path); 

、ここでの事はどのように見えるかです。私はあなたがお勧めですかモバイル、上で実行するつもりです場合

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

おかげで、:

をダウンロードして保存:

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

ロードあなたは、いくつかの変更を行う必要があるかもしれません保存するには? persistentDataPath?また、もし私が私のassetbundleの古いバージョンを交換したいのであれば、それは問題になるでしょう。たとえば、私はassetbundle1_01を持っていました。そして、ユーザーが書き込み許可を与えないと、それが問題になるでしょうか? – ywj7931

+0

はい、persistentDataPathを使用してください。あなたは**この中にフォルダを作成し、そのフォルダに保存しなければなりません。 persistentDataPathパスに直接保存しないでください。それは 'Application.persistentDataPath/yourfolder/filename.ext'でなければなりません。あなたは常にファイルを新しいバージョンに置き換えることができます。最後に、save関数に 'try'と' catch'コードがあります。例外がある場合は、catch'ブロックでチェックし、書き込み権限を有効にするようにユーザーに依頼することができます。あなたが欲しくない人のデバイスに書き込むことはできません。 – Programmer

+0

Androidで問題が発生した場合は、「書き込み許可」を「外部(SDカード)」に変更してください。 – Programmer

関連する問題