Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

[UnityWebRequest / DownloadHandler] AssetBundle null on Android. Works in Editor and PC application

Discussion in 'Scripting' started by ST_ProductVIz, Oct 4, 2018.

  1. ST_ProductVIz


    Nov 29, 2017
    Hey Guys,

    "DownloadHandlerAssetBundle.GetContent(UnityWebRequest)" returns "NULL" as deployed android application, but works in Editor and as deployed PC-Standalone application...

    I am now struggling for a while(2 weeks) with a strange behaviour, maybe you can help me out...

    I am trying to load an asset at runtime from a webserver and instantiate it. So far, thats it.

    For the sake of completeness, i am using this code for creating my assetBundle from a simple sphere/cube (which i used to make a simple prefab).

    Code (CSharp):
    2. using UnityEditor;
    3. using UnityEngine;
    5. namespace Assets.Editor
    6. {
    7.     public class CreateAssetBundles : MonoBehaviour
    8.     {
    9.         [MenuItem("Assets/Build AssetBundles")]
    10.         static void BuildAssetBundles()
    11.         {
    12.             BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    13.         }
    14.     }
    15. }
    I am uploading this asset on a webserver(Azure File Storage) and using the following script to load it during runtime. This is the point where it gets tricky...

    Code (CSharp):
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    6. namespace Assets
    7. {
    8.     public class WebRequest : MonoBehaviour
    9.     {      
    10.         void Start()
    11.         {
    12.             StartCoroutine(GetRequest(""));
    13.         }
    15.         IEnumerator GetRequest(string uri)
    16.         {
    17.             //UnityWebRequest uwr = UnityWebRequest.Get(uri);
    18.             UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(uri);
    19.             yield return uwr.SendWebRequest();
    21.             if (uwr.isDone && uwr.error == null)
    22.             {
    23.                 var assetBundle = DownloadHandlerAssetBundle.GetContent(uwr);
    24.                 Debug.Log("Asset Received");
    25.             }
    26.             else
    27.             {
    28.                 Debug.Log("Error While loading: " + uwr.error);
    29.             }
    30.         }
    31.     }
    32. }
    For some reason, this code works perfectly inside the Editor or as deployed PC Standalone application but i won't receive the asset on my deployed Android application. The returned assetBundle from the DownloadHandler is null on Android, whereas i am receiving ang object in Editor and PC...

    I have internet access on my mobile phone and i also tried different phones (GalaxyS8 and Xiaomi 5S).

    As you see on the screenshots below, the webrequest properties "error" is false and "isDone" is true... I also received the correct amount of Bytes (28148), with "isHttpError" and "IsNetworkError" false and a "ResponseCode" of 200, which seems valid/the same values as compared with teh Editor/PC-standalone application...

    The only thing which is suspicious that the "uploadProgress" and the "downloadProgress" on android is 0 compared to the Editor/PC (1)...
    looks like there is something wrong with the upload and the download of the request...

    but why is both zero?

    I would agree with the zero in the upload request, if i wouldn't receive anything from the server, but i got an answer from the server and in addition 28148 bytes. So at least the uploadProgress has to be 1. And compared to the amount of Bytes received the downloadProgress should be 1 too...

    I tried it with Unity 2018.2.8f1 and 2018.2.10f1. On my PC i am using Windows 10 with the latest updates and the Company Wifi. On my Galaxy S8 i have latest available firmware (Android 8.0) with the latest Updates and using a Guest Wifi. On my Xiaomi 5S i have Android 8.1 (nightly build Lineage
    15.1) with mobile data connection.

    Based on the different mobile phones and the different internet connection i think we can exclude the internet connection, more or less the phone firmware and the webserver(azure cloud storage) as the reason for the missing asset on android...

    I assume, that it is a bug in Unity... but i am astonished, that no one else has problems with this fundamental thing... this makes me pondering...

    I appreciate any suggestions to make this simple webRequest work on android...

    Many thanks in advance...

    Ps.: sorry for the long post, but i wanted to make clear which things i already tried and give you as much information as possible... if you need further informations don't hesitate to ask me ;)

    Attached Files:

  2. MaskedMouse


    Jul 8, 2014
    This may be a really stupid question but did you consider building the asset bundles for BuildTarget.Android?
    Every platform has its own bundles
  3. Brathnann


    Aug 12, 2014
    As MaskedMouse mentioned, you need android assetbundles for android. The window assetbundles don't work for Android.
  4. ST_ProductVIz


    Nov 29, 2017
    @MaskedMouse thank you very much for this hint, this was the reason. now its working.
    i completely forgot to check this part of the code and didn't remember i chose a build target for the assets and beside this i also haven't thought that it makes a difference on viewing an asset on android or windows...
  5. Brathnann


    Aug 12, 2014
    You may consider using the assetbundle browser, which should streamline the process of creating bundles.
  6. turbolek


    Dec 30, 2016
    I have just discovered, that on Android asset bundle names are case sensitive, while on PC they are not.
    So when your bundle is called say "mybundle", trying to load "MyBundle" will work on PC, but won't work on Android. Loading "mybundle" should work on both platforms.
  7. mnievas


    Apr 22, 2015
    THAT Was my Issue... Thank you so much...!!!! It was on windows.