2017-10-31 3 views
1

iOSビルドでUnityアセットバンドルを動作させることができません。ユニティでユニティにアセットバンドルをビルドしてロードする

私はassetbundles構築:

using UnityEditor; 

public class CreateAssetBundles 
{ 
    [MenuItem("Assets/Build AssetBundles")] 
    static void BuildAllAssetBundles() 
    { 
     BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.iOS); 
    } 
} 

をそして彼らはユニティで正常に動作。

AssetBundle bundleLoadRequest = AssetBundle.LoadFromFile("file://" + Application.dataPath + "/AssetBundles/iOS/" + myassetbundlename.ToString()); 

および/または

WWW wwww = WWW.LoadFromCacheOrDownload("file://" + Application.dataPath + "/AssetBundles/iOS/" + myassetbundlename.ToString(), 4); 

でそれらを使用して:私はプロジェクトをビルド

(なければ "ファイル//" 接頭辞、バンドルはユニティもXcodeで動作しません) Xcodeで実行し、このエラーを受け取ります。

Unable to open archive file: /Users/user/Documents/Workspaces/unityproject/Assets/AssetBundles/iOS/lchairanimations

正しいパスを設定すること後でassetbundleフォルダをXcodeプロジェクトにコピーしたので、問題は解決されません。

+0

あなたは、インターネットからこのassetundleをダウンロードしていますか? – Programmer

+0

いいえ、私はローカルにアクセスしようとしています。 LoadFromCacheOrDownloadとAssetBundle.LoadFromFileの両方を試してみましたが、Unityビルドで絶対パスを使用しようとしました。何らかの理由でXcodeがバンドルを見つけられない、または開くことができません。 – Gideons

+0

私の回答はあなたの問題を解決しましたか?私はそれが期待されます。私に教えてください – Programmer

答えて

2

AssetBundleをビルドするときは、StreamingAssetsフォルダに配置する必要があります。 AssetsフォルダにStreamingAssetsフォルダをまだ作成していない場合は作成します。その中にAssetBundleという別のフォルダを作成します。これは、StreamingAssetsフォルダにあるものを整理するためだけのものです。

最終パスはAssets/StreamingAssets/AssetBundleである必要があります。

Application.streamingAssetsPathを使用してStreamingAssetsフォルダにアクセスする必要があります。すべてのフォルダを使用してアクセスするに

Application.streamingAssetsPath + "/AssetBundle/" + assetbunlenameWithoutExtension;

なお、以下のコードではなく、それを使用する必要がありますので、パス名を組み合わせることPath.Combineを使用することをお勧めします。

ビルドスクリプト

public class ExportAssetBundles 
{ 
    [MenuItem("Assets/Build AssetBundle")] 
    static void ExportResource() 
    { 
     string folderName = "AssetBundles"; 
     string filePath = Path.Combine(Application.streamingAssetsPath, folderName); 

     BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.iOS); 
    } 
} 

あなたのロードスクリプト

IEnumerator loadAsset(string assetBundleName, string objectNameToLoad) 
{ 
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundles"); 
    filePath = System.IO.Path.Combine(filePath, assetBundleName); 

    var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath); 
    yield return assetBundleCreateRequest; 

    AssetBundle asseBundle = assetBundleCreateRequest.assetBundle; 

    AssetBundleRequest asset = asseBundle.LoadAssetAsync<GameObject>(objectNameToLoad); 
    yield return asset; 

    GameObject loadedAsset = asset.asset as GameObject; 
    //Do something with the loaded loadedAsset object 
} 

USAGE

。上記の最初のスクリプト(ExportAssetBundles)を使用して、資産 - >Build AssetBundleメニューにアクセスして、AssetBudleを構築します。

Assets/StreamingAssets/AssetBundlesディレクトリには、構築済みのAssetBundlesが表示されます。

。以下の仮定を下にしてみましょう:

アセットバンドルの名前はanimalsです。

b。動物のAssetbundleからロードしたいオブジェクトの名前はdogです。

enter image description here

ロードは、このように簡単です:

string nameOfAssetBundle = "animals"; 
string nameOfObjectToLoad = "dog"; 

StartCoroutine(loadAsset(nameOfAssetBundle, nameOfObjectToLoad)); 
関連する問題