【问题标题】:Loading assetbundle from root of sdcard从 sdcard 的根目录加载assetbundle
【发布时间】:2018-06-19 15:05:23
【问题描述】:

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

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

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

【问题讨论】:

标签: android unity3d assetbundle


【解决方案1】:

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>

当我从 &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" /&gt; 中删除 android:maxSdkVersion="18" 时,我解决了这个问题。

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

【讨论】:

    【解决方案2】:

    如果您想直接访问资产包,请使用 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;
    

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 1970-01-01
      • 2013-10-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多