【问题标题】:Why can I not load assets via Resource.LoadAll() at runtime when running on IOS but the assets load fine on OS X?为什么在 IOS 上运行时无法在运行时通过 Resource.LoadAll() 加载资产,但在 OS X 上加载正常?
【发布时间】:2023-03-28 06:44:01
【问题描述】:

在努力从资源中加载资产之后(我仍然没有弄清楚为什么这实际上不起作用),在使用以下代码将资产打包到 AssetBundles 之后,我终于能够在运行时加载资产:

var myBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/soundanchors");
  if (myBundle == null)
  {
     Debug.Log("Failed to load assets from bundle at Assets/AssetBundles");
  }
  else
  {
     Debug.Log("Successfully loaded sound anchors from Assets/AssetBundles");
     soundAnchors = myBundle.LoadAllAssets();
     AddAnchorsToGrid();
  }

当我在 OS X 上构建和运行项目时,代码运行良好。但是,一旦我构建项目并在 IOS 设备上运行它,我就不再看到资产包加载了。

我假设这是因为在 IOS 上保存捆绑包的路径不同,但我无法找到应该查找它们的位置。

从代码中可以看出,我的 Assets 文件夹中包的位置在这里:“Assets/AssetBundles/soundanchors”

Unity 如何处理 IOS 上的资产路径? Unity API 中是否存在一些包含默认 IOS 路径前缀的常量?

【问题讨论】:

标签: ios unity3d assetbundle


【解决方案1】:

据我所知,对于 IOS,您必须在 Assets 中创建名为 StreamingAssets 的文件夹,例如“Assets/StreamingAssets”,并将您的捆绑包放入其中。 您可以像这样加载您的资产包:

void Start()
{
    var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
    if (myLoadedAssetBundle == null)
    {
        Debug.Log("Failed to load AssetBundle!");
        return;
    }

    var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("MyObject");
    Instantiate(prefab);

    myLoadedAssetBundle.Unload(false);
}

【讨论】:

  • 我确实知道了,但我不再记得解决方案是什么了......在 SO 上处理这些类型的情况的协议是什么?
猜你喜欢
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多