Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android isn't able to load Asset Bundles and I'm using Unity v5.1.1f1

Discussion in 'Android' started by pyrt0, Oct 1, 2015.

  1. pyrt0

    pyrt0

    Joined:
    Aug 17, 2012
    Posts:
    3
    Hey all,
    I'm developing an app for Android, and am currently providing the user with the option to download models to their Android device. Everything works on desktop as designed, however on Android devices I get an error:
    "....can't be loaded because it was not built with the right version or build target."
    I'm using Unity v5.1.1f1, and the models are being built and exported using the same version.

    Everything I've read states that all you have to do is identify the Buid Target (i.e. BuildTarget.Android in this case) in the ExportAssetBundles (Editor) Script.

    Here is my code I'm using to export my Asset Bundle with "No Dependency Tracking":
    [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
    static void ExportResourceNoTrack () {
    // 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.
    BuildPipeline.BuildAssetBundle(Selection.activeObject,
    Selection.objects, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);
    }
    }

    Here is my code that downloads the model and saves it to the mobile device:
    string savePath = Application.persistentDataPath + saveToPath;
    if (!Directory.Exists(savePath))
    Directory.CreateDirectory(savePath);

    string filePath = Application.persistentDataPath + saveToPath + saveAs + ".unity3d";

    if (!File.Exists(filePath)) {
    File.WriteAllBytes(filePath, www.bytes);
    } else {
    File.Delete(filePath);
    File.WriteAllBytes(filePath, www.bytes);
    }

    And here is my script that I load the Asset Bundle from the phone:
    // This loads a model from the prefabs directory
    GameObject obj = Resources.Load("Prefabs/" + thisContainer.modelName) as GameObject;
    GameObject modelLoaded;

    if (obj == null) {

    // Attempt to load model from dynamically added directory on mobile device called "Saved Prefabs" (i.e. string saveToPath = "/SavedPrefabs/")
    string assetBundleName = Application.persistentDataPath + saveToPath + thisContainer.modelName + ".unity3d";

    AssetBundle assetBundle = new WWW("file:///" + assetBundleName).assetBundle;

    if (assetBundle != null) {
    obj = assetBundle.mainAsset as GameObject;
    Instantiate(obj);


    Analysis:
    I've found that the model is downloaded fine, I've even copied the downloaded file from my phone to my desktop, ran my Unity project and it opened without any issues. Essentially verifying that this is not a Asset Bundle export issue, but rather a mobile-only issue when I'm writing the data to the phone.

    How can I write/save my downloaded Asset Bundle to the mobile device (Android) properly so I can load my models already!!!
     
  2. ricardo_arango

    ricardo_arango

    Unity Technologies

    Joined:
    Jun 18, 2009
    Posts:
    64
    Try yielding to complete the download operation:

    Code (csharp):
    1.  
    2. WWW www = new WWW("file:///" + assetBundleName);
    3. yield return www;
    4. if(www.error == null){
    5.    AssetBundle bundle = www.assetBundle;
    6.    if (assetBundle != null) {
    7.       obj = assetBundle.mainAsset as GameObject;
    8.       Instantiate(obj);
    9.    }
    10.    else
    11.       Debug.Log ("AssetBundle not loaded");
    12. }
    13. else
    14.    Debug.Log (www.error);
    15.  
     
  3. pyrt0

    pyrt0

    Joined:
    Aug 17, 2012
    Posts:
    3
    Why, does that change the formatting of the Asset Bundle so it can
     
  4. pyrt0

    pyrt0

    Joined:
    Aug 17, 2012
    Posts:
    3
    I meant to say, I've already tried this and it still produces the same error. I don't think this is the issue since I've been able to download an Asset Bundle to my mobile device, move that file to my desktop environment, and successfully opened it there! In fact, I've tried the exact same code above that you posted, but I still get the same mobile error.
    That being said, do you still think it's a simple "yield" line of code?