unity3d - WWW.LoadFromCacheOrDownload is not working in augmented reality app on android phone using unity


Keywords:android 


Question: 

I am trying to build an augmented reality app and in that app i want some prefabs to load dynamically. So that with some 3d model prefabs on unity store i created myasset.unity3d . I am using WWW.LoadFromCacheOrDownload to download that unity3d file and using it.

When i am debugging in my laptop the file is downloading and i am using it as i want but when i add build in and try it on android phone download is not happening.

The code i am using to download is

IEnumerator DownloadAndCache() {
         while (!Caching.ready)
             yield return null;

         string bundleURL = "http://s3-ap-southeast-1.amazonaws.com/cf-export/sofa.unity3d";

         www = WWW.LoadFromCacheOrDownload (bundleURL, 1);
         yield return www;

         Debug.Log (www.assetBundle);

         AssetBundle bundle = www.assetBundle;
 }

So please help me .

i also tried other way of doing it but still i did not succeed

IEnumerator Start () {
    WWW www = WWW.LoadFromCacheOrDownload (BundleURL, 1);
    yield return www;
    AssetBundle bundle = www.assetBundle;
    AssetBundleRequest request = bundle.LoadAssetAsync (AssetName, typeof(GameObject));
    yield return request;
    GameObject obj = request.asset as GameObject;
    Instantiate (obj);
    bundle.Unload(false);
    www.Dispose();
}

The above two methods are working in when testing in unity IDE but if i build using android and install it in android device its not working. I gave internet access to app while building

I printed the error using a text in mobile and please check the screen shot attached below enter image description here

the first error text is www (variable for downloading) and second is www.error ( actual error). i did not get what that error meant. So please help me


3 Answers: 

the issue is asset bundle version is not sync with app version. So while making asset bundle i added small code which made my code working. To make Asset bundle my code is

using UnityEditor;

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

To use AssetBuundle for Mobile platform:

  1. While creating AssetBundle it must be platform specific. For Android use following Script to create AssetBundle. put it inside "Editor"

     using UnityEngine;
        using System.Collections;
        using UnityEditor;
        public class ExportAssetBundles : Editor {
            [MenuItem("Assets/Build AssetBundle")]
            static void ExportResource()
            {
                string path = "Assets/AssetBundle/myAssetBundle.unity3d";
                Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
                BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,
                                               BuildAssetBundleOptions.CollectDependencies
                                             | BuildAssetBundleOptions.CompleteAssets,BuildTarget.Android);
            }
        }
    
    1. Then Create folder "AssetBundle" inside Asset

      When you create Assetbundle the AssetBundle.unity3d file will store inside this folder


    Now you need to use Created Assetbundle at runtime. For this,

    1. Write a script AssetBundleAugmenter

      public class AssetBundleAugmenter : MonoBehaviour {
      
      public string AssetName;
      public int Version;
      private GameObject mBundleInstance = null;
      private bool mAttached = false;
      void Start() {
          StartCoroutine(DownloadAndCache());
      
      }
      // Update is called once per frame
      IEnumerator DownloadAndCache() {
          while(!Caching.ready)
              yield return null;
          //you can use remote URL like: www.arprabhu.com/assetBundle OR Local URL
          // example URL of file on PC filesystem (Windows)
          // string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d";
          // example URL of file on Android device SD-card
      
          string bundleURL = System.IO.File.ReadAllText("/mnt/sdcard/filepath.txt");
          Debug.Log ("Asset Loaded");
          using (WWW www = WWW .LoadFromCacheOrDownload(bundleURL, Version)) {
              yield return www;
              if (www .error != null)
                  throw new UnityException("WWW Download had an error: " + www .error);
              AssetBundle bundle = www .assetBundle;
      
              if (AssetName == "")
                  Instantiate(bundle.mainAsset);
              else
                  Instantiate(bundle.LoadAsset(AssetName));
              // Unload the AssetBundles compressed contents to conserve memory
              bundle.Unload(false);
      
          }
      }}
      
  2. Attach this Script to Empty GameObject in your Scene


Congrats!, you are ready to go.

 

Try checking your androidManifest.xml for the permission to connect to external servers. Then, if everything seems alright and still doesn't work, my advice is to download the Device Console plugin for the Unity Editor, which is a really nice way to access the internal Android Debugger.

Or if you're truly brave: use logcat within the terminal...