从sdcard的根目录加载assetbundle

时间:2018-02-19 12:09:54

标签: android unity3d assetbundle

我正在尝试从内部存储中读取资产包。这有效:

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);
}

但是,当我尝试将其重定向到root时,就像这样:

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);
}

我在logcat上得到Unable to open archive file: /storage/emulated/0/AssetBundles/model_objs

捆绑包完全相同,位于两个位置

2 个答案:

答案 0 :(得分:0)

Colin Young是对的,我在自动生成的清单上遇到了问题。默认清单如下:

<?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>

当我从android:maxSdkVersion="18"移除<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />时,我解决了这个问题。

要获取默认清单,我构建了我的项目并转到{PROJECT DIRECTORY}\Temp\StagingArea\AndroidManifest.xml。然后,我在我的Unity项目中创建了以下嵌套文件夹:Assets/Plugins/Android/并将上述文件粘贴到更改中。

答案 1 :(得分:0)

如果要直接访问资产包,请使用LoadFromFileAsync而不是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;