Unity:从没有文件扩展名的资产包中加载资产

Unity: Loading an asset from an Asset Bundle without a file extention

我正在使用资产包来加载本地化的 VO。这些 VO 文件可以是 .wavs 或 .oggs,并且在加载文件之前指定是不可行的。这在从资源加载默认文件时很好,因为 Unity 不需要文件扩展名。但是,当从 Asset Bundle 加载本地化文件时,如果加载调用中未包含文件扩展名,则无法找到该文件。清单文件确实包含扩展名。

有没有办法在不提供扩展名的情况下从 Asset Bundle 加载文件?据我所知,这在 Unity 4 中是可行的,但我在使用 Unity 5 (5.1.2p3) 时遇到了问题。

举个我正在尝试做的例子:

这个有效:

AudioClip soundClip = localisedAssetBundle.LoadAsset<AudioClip>( "sound.wav" );

这也有效:

AudioClip soundClip = Resources.Load<AudioClip>( "sound" );

但这不是:

AudioClip soundClip = localisedAssetBundle.LoadAsset<AudioClip>( "sound" );

POST-答案编辑:

我的例子不太正确,因为我是在转述它们。第三个例子实际上会像上面写的那样工作。然而,我在我的代码中实际尝试做的是:

AudioClip soundClip = localisedAssetBundle.LoadAsset<AudioClip>( "Assets/sound" );

因为我既没有指定有效路径,也没有指定有效文件名,所以这不起作用。请参阅已接受的答案以获得完整解释。

您可以通过资产包的路径或名称从资产包中加载资产。

来自文档:

AssetBundle.LoadAsset will load an object using its name identifier as a parameter. The name is the one visible in the Project view. You can optionally pass an object type as an argument to the Load method to make sure the object loaded is of a specific type.

例如:

AudioClip sound = _assetBundle.LoadAsset<AudioClip>( "assets/sound.wav" ); // Get an asset by path

AudioClip sound2 = _assetBundle.LoadAsset<AudioClip>( "sound" ); // Get an asset by name
AudioClip sound = _assetBundle.LoadAsset<AudioClip>( "sound.wav" ); //Get an asset by file name

AudioClip sound = _assetBundle.LoadAsset<AudioClip>( "Assets/sound" ); // This will not work as it's neither a path, nor an asset name

如果您有 2 个名称相同的资产,您可以指定您需要的资产类型。假设您有一个名为 "test" 的 AudioClip 资产和一个名为 "test".

的 material 资产
bundle.LoadAsset<Material>("test"); //returns you material
bundle.LoadAsset("test", typeof(Material)); //as well, as this one
bundle.LoadAsset<AudioClip>("test"); //returns an audio clip