edit: I’ve solved this now I think. What I had done wrong was create the assets badly. I don’t know the exact reasons, but taking the standard unity export asset bundle script and adding the BuildTarget.iPhone (I had originally left out a couple important parameters)
I’ve included some asset bundles in the “StreamingAssets” folder. They load fine in the editor, but, although I believe I have their path correct, they fail in iOS.
If I build the asset bundles with their target set to WebPlayer I get this error from the device:
The file can not be loaded because it was created for another build target that is not compatible with this platform.
Please make sure to build asset bundles using the build target platform that it is used by.
(Filename: /Applications/buildAgent/work/f724c1acfee760b6/Projects/…/Runtime/Serialize/SerializedFile.cpp Line: 210)
The asset bundle ‘file:///var/mobile/Applications/5AB67DD7-DDA0-44EA-8E6E-DBCDE54E18E2/prpg.app/Data/Raw/AssetBundles/LevelGeneratorCrypt1.unity3d’ can’t be loaded because it was not built with the right version or build target.
But when I build them with their bundles set to iPhone the device just says : Failed to load Asset Bundle. And it does not tell me why.
I’m going to try exporting Asset Bundles in different ways, see if I get lucky.
But does anyone know what am I missing? Is there a guide to getting Asset Bundles working in iOS? (I’m pretty certain I’ve got the dataPath correct)
My problem was quite bizarre in the end, actually probably a bug in Unity:
This works:
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies iPhone")]
static void ExportAssetBundleIOSComplete() {
// Bring up save panel
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0) {
// Build the resource file from the active selection.
//
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
Selection.objects = selection;
//
}
}
While this creates a Asset Bundle that can’t be loaded from iOS (the only difference being it does not get a path from the save panel):
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies iPhone (no prompt)")]
static void ExportAssetBundleIOSCompleteNoPrompt() {
// Do not bring up save panel
string path = Application.dataPath + "/StreamingAssets/AssetBundles/" + Selection.activeObject.name + ".unity3D";
if (path.Length != 0) {
// Build the resource file from the active selection.
//
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.iPhone);
Selection.objects = selection;
//
}
}
Also it looks like Asset Bundles have a large memory overhead, so the whole affair was quite futile in the end.
thanks for this information. Im testing this now for android (using the android build target for the assets) so hopefully this ends a problem i was having.
Thank you very much Evan, you just saved my bacon!
The above code is working properly in iphone.
Can anyone help me , how to create asset bundle for Android ?
Thanks in advance.
@VamshiKrishnaP,
just change build target to android.
ok i will try with this. thank you @UnityCoder
@unitycoder are you from india ? from where ?
@Vamshi
Yes i am from India and please check pm