android - Loading assetbundle from root of sdcard


Keywords:android 


Question: 

I am trying to read an assetbundle from internal storage. This works:

void Start()
{
    var myLoadedAssetBundle = AssetBundle.LoadFromFile(Application.persistentDataPath + "/AssetBundles/model_objs");
    if (myLoadedAssetBundle == null)
    {
        Debug.Log("Failed to load AssetBundle!");
        return;
    }

    GameObject wheel = (GameObject)myLoadedAssetBundle.LoadAsset("01_obj");
    Instantiate(wheel);
}

However when I try to redirect it to root, like so:

void Start()
{
    var myLoadedAssetBundle = AssetBundle.LoadFromFile("/storage/emulated/0/AssetBundles/model_objs");
    if (myLoadedAssetBundle == null)
    {
        Debug.Log("Failed to load AssetBundle!");
        return;
    }

    GameObject wheel = (GameObject)myLoadedAssetBundle.LoadAsset("01_obj");
    Instantiate(wheel);
}

I get a Unable to open archive file: /storage/emulated/0/AssetBundles/model_objs on a logcat

The bundle is exactly the same and is in both locations


2 Answers: 

Colin Young was right, I had an issue with the auto-generated manifest. The default manifest was as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.demo.relativeAssets" xmlns:tools="http://schemas.android.com/tools" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:isGame="true" android:banner="@drawable/app_banner">
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <meta-data android:name="unity.build-id" android:value="11967537-6671-47db-ad0a-c4fc8a8d10c0" />
    <meta-data android:name="unity.splash-mode" android:value="0" />
    <meta-data android:name="unity.splash-enable" android:value="True" />
    <meta-data android:name="android.max_aspect" android:value="2.1" />
  </application>
  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="26" />
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
  <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

When I removed android:maxSdkVersion="18" from <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" /> I solved the issue.

To get the default manifest, I built my project and went to {PROJECT DIRECTORY}\Temp\StagingArea\AndroidManifest.xml. I then created the following nested folders in my unity project: Assets/Plugins/Android/ and pasted the aforementioned file with the changes.

 

If you want to access an asset bundle directly then use LoadFromFileAsync instead of LoadFromFile :

        private AssetBundleRequest bundleRequest;
        private string yourAssetBundle;    //name of your asset bundle

        string path = "/storage/emulated/0/AssetBundles/" + yourAssetBundle;
        //Please note that while doing this you must have a folder named "AssetBundles" in your
        //root directory with your asset "yourAssetBundle" existing in it.

        AssetBundleCreateRequest loadedAssetbundle = AssetBundle.LoadFromFileAsync(path);
        bundleRequest = loadedAssetbundle.assetBundle.LoadAssetAsync<GameObject> (yourAssetBundle);

        //further you can instantiate it as a gameobject
        GameObject augmentedObj = Instantiate (bundleRequest.asset, Vector3.zero, Quaternion.identity) as GameObject;