Search Unity

AssetBundle.LoadFromFile returns NULL

Discussion in 'Asset Bundles' started by Shefich, Mar 20, 2020.

  1. Shefich

    Shefich

    Joined:
    May 23, 2013
    Posts:
    143
    Hi,

    I'm trying to load asset bundles on Android platform.
    Everything goes fine on 99,9% devices, but there is small amount of devices, which gives me the error.
    Devices: Samsung, Xiaomi, Huaiwei, motorola, etc.
    OS: 4,5,6,7,8,9,10

    I can't get the error on my test devices, so I am asking for help here.

    Here's the logic of loading the AssetBundles:

    First I am trying to load them async:
    Code (CSharp):
    1. IEnumerator LoadDatabasesAsync ()
    2.     {
    3.         AssetBundleCreateRequest bundleReq = AssetBundle.LoadFromFileAsync(
    4. Path.Combine(Application.streamingAssetsPath, "androidBundles", "data"));
    5.         yield return bundleReq;
    6.  
    7.         if (bundleReq == null)
    8.         {
    9.             LoadDataSync();
    10.         } else
    11.         {
    12.             if (bundleReq.assetBundle == null)
    13.             {
    14.                 LoadDataSync();
    15.             } else
    16.             {
    17.                 TextAsset textAsset = bundleReq.assetBundle.LoadAsset<TextAsset>("database");
    18.                 // here textAsset = NULL, if I don't use above if statement to check request for NULL
    19.                 bundleReq.assetBundle.Unload(false);
    20.             }
    21.         }
    22.     }

    If it fails, I am using sync method, just to quickly check AssetBundle for empty data:
    Code (CSharp):
    1. void LoadDataSync ()
    2.     {
    3.         string assetBundlesPath = Path.Combine(Application.streamingAssetsPath, "data");
    4.         AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundlesPath);
    5.         if (assetBundle == null) // So it gives NULL here
    6.         {
    7.             //I'm trying to change path code
    8.             assetBundlesPath = "jar:file://" + Application.dataPath + "!/assets/androidBundles/data";
    9.             assetBundle = AssetBundle.LoadFromFile(assetBundlesPath);
    10.             if (assetBundle == null) // It gives NULL here
    11.             {
    12.                 //Just to test if the issue is with 2 "/" symbols
    13.                 assetBundlesPath = "jar:file:///" + Application.dataPath + "!/assets/androidBundles/data";
    14.                 assetBundle = AssetBundle.LoadFromFile(assetBundlesPath);
    15.                 if (assetBundle == null) // It gives NULL here
    16.                 {
    17.                     //Don't know what's the problem
    18.                 }
    19.             }
    20.         }
    21.         TextAsset textAsset = assetBundle.LoadAsset<TextAsset>("database"); // here textAsset = NULL
    22.     }
    I am using Unity 2018.4.18f1, but the issue also happens on older versions.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    I recommend avoiding mixed case in file/folder names. Android file system is case sensitive, easy to make a mistake, especially since Unity produces asset bundles with all lowercase letters in their names.
    In your very first load, is your asset bundle file really named "data" (no extension whatsoever)?
    Your send synchronous LoadFromFile uses different location (no "androidBundles" part).
    The remaining two are incorrect. Application.dataPath should already have jar:file:// at the start.
     
  3. Shefich

    Shefich

    Joined:
    May 23, 2013
    Posts:
    143
    Hi Aurimas, thank you for your answer.

    - Yes, my assetbundles have no extension. Even the path(the first one and the others) gives me no such folder error.

    - As I can see on unity site here: https://docs.unity3d.com/ru/current/Manual/StreamingAssets.html
    The path should include "jar:file://" as I am using Application.dataPath. Also I can see the right representation of the path in logs.

    The main trouble here is that I can not understand, why there are no assets in any case. I have right path, I even checked various path variants and there is nothing.
    Can you give me advice on checking some other paths? Maybe if user have installed application on sd card, then moved it to phone storage or vice-versa, there can be any troubles in loading assets? Or maybe user couldn't somehow provide rights to store assetbundles on device? (on android 9 and 10?)