Iago Iago - 4 months ago 34
iOS Question

Assetbundles not working with iOS

I am developing an AR app for the iPhone in Unity which downloads asset bundles from a webserver and attaches them to image targets. So far almost everything is working--except that when the app tries to load the asset bundle, I get the following error:


'asset/bundle/url' can't be loaded because it was not built with the right version or build target.


I created the assetbundles using the following script, modified slightly from the Unity documentation:

using UnityEditor;

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


I then uploaded the assetbundles to the server using scp. The app downloads the assetbundles fine but cannot load them. Here's the code:

IEnumerator DownloadAndCache (string username, string modelName){
// Wait for the Caching system to be ready
while (!Caching.ready) {
Debug.Log ("Waiting on caching");
yield return null;
}

//Compute request url for server
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "http";
uriBuilder.Host = BaseURL;
uriBuilder.Path = username.Trim() + "/" + modelName.Trim();
string BundleURL = uriBuilder.ToString ();
Debug.Log ("Attempting to download " + BundleURL);

// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
yield return www;
if (www.error != null) {
Debug.Log ("Download error");
throw new Exception ("WWW download had an error:" + www.error);
}
AssetBundle bundle = www.assetBundle;
Debug.Log ("Got assets "+string.Join(",", bundle.GetAllAssetNames ()));
GameObject loadedModel = Instantiate(bundle.LoadAllAssets()[0]) as GameObject;

//attach to the image target
loadedModel.transform.parent = ImageTargetTemplate.gameObject.transform;
loadedModel.transform.position = new Vector3 (0, 0, 0);
loadedModel.transform.localScale = new Vector3 (1, 1, 1);

// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);

} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}


So far I have tried:


  • Setting the build target BuildTarget.iPhone - no difference

  • Building the assetbundles using the assetbundle manager API from Unity

  • Unchecking "strip engine code" in the build options for iOS - I heard on another forum that this can cause problems.



Any help appreciated.

-UPDATE-

I modified the BuildAssetBundle script per Programmer's suggestion and switched the target platform from "iPhone" to "Universal" in the player settings panel. I'm pretty sure this is what resolved the problem.

Answer

This means that your target device was not iOS when you built the Asset bundle. Change your target to iOS then rebuild the Asset Bundle.

Recompile and re-upload to iOS and that should solve your problem.

If that didnt work then... If you built your AssetBundle while the scripting backend was set to IL2CPP, but then you use them with the .NET scripting backend, that error would occur too. Solution is to rebuild your Asset Bundle after changing to .NET scripting backend through the player settings.

Also try replacing BuildTarget.iPhone with EditorUserBuildSettings.activeBuildTarget so that that Bundle would automatically build for any build target currently selected.