1

I'm using asset bundles to load localised VO. These VO files could be .wavs or .oggs and it's not viable to specify which before loading the file. This is fine when loading the default files from Resources since Unity doesn't require the file extension. However when loading the localised files from an Asset Bundle, if the file extension isn't included in the load call the file can't be found. The manifest file does include the extension.

Is there a way to load a file from an Asset Bundle without providing the extension? From what I hear, this was doable in Unity 4 but I'm having the problem using Unity 5 (5.1.2p3).

Just as an example of what I'm trying to do:

This works:

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

This also works:

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

But this doesn't:

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

POST-ANSWER EDIT:

My examples weren't quite correct, as I was paraphrasing them. The third example actually would have worked as it is written above. However, what I had actually tried to do in my code was this:

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

Since I was neither specifying a valid path, nor a valid filename, this didn't work. See accepted answer for full explanation.

Jean Finley
  • 507
  • 3
  • 9
  • 21
  • I am probably missing some context here. Don't you load asset from an asset bundle by their name, like in AssetBundle.LoadAsset()? – buxter Aug 27 '15 at 09:27
  • Yup. Problem is the name that gets passed in. I want to be able to do LoadAsset( "sound" ) instead of LoadAsset( "sound.wav" ). You can do that if you're just loading from the Resources folder (Unity seems to implicitly add file extensions until it finds a file. Not entirely sure how it works but it does) but if you try to do that when loading from an asset bundle it won't find the file. – Jean Finley Aug 27 '15 at 11:20
  • This is really weird, because I've just tried it and bundle.LoadAsset("test"); works for me. May I ask how do you build the bundles? – buxter Aug 27 '15 at 15:33
  • Weird... Which version of Unity are you using? – Jean Finley Aug 28 '15 at 07:51
  • I'm using the editor script from the Documentation which just calls "BuildPipeline.BuildAssetBundles( "AssetBundles", new BuildAssetBundleOptions(), EditorUserBuildSettings.activeBuildTarget );" (the one in the docs just uses the path param, I added the other two so it'd stop switching platforms every time i built). I select the assets I want in the bundle from the project panel, select the asset bundle I want in the inspector, right click, click build assets, upload the resulting bundle to the server to be downloaded. – Jean Finley Aug 28 '15 at 07:56
  • Version - same as yours. Do you set the variants together with a bundle name? This can be a problem. – buxter Aug 28 '15 at 08:14
  • Don't quite understand the question. At the moment since I'm testing I'm selecting a non-branching tree of about 3 folders with a file at the end and setting the asset bundle on all of them if that's what you mean. The asset bundle seems to be properly set on the file, and the file appears in the asset bundle manifest with the correct folder structure... – Jean Finley Aug 28 '15 at 11:17
  • I am talking about this: http://docs.unity3d.com/Manual/BuildingAssetBundles5x.html Check the sectoin called AssetBundle Variants. Can it also happen that "sound.wav" is a file name, not an extention? (If you e.g stand on the asset in Project view, it's displayed as sound.wav.wav at the very bottom of the window) – buxter Aug 28 '15 at 12:13
  • Ah, sorry. No, the variants are set to 'None'. At the very bottom of the window the file is displayed as 'sound' with no extension at all. – Jean Finley Aug 28 '15 at 14:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88216/discussion-between-buxter-and-jean-finley). – buxter Aug 28 '15 at 15:06

1 Answers1

3

You can load an asset from an asset bundle either by its path, or by its name.

From docs:

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.

For example:

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

In case you have 2 assets with identical name, you can specify which asset type you need. Let's say you have an AudioClip asset called "test" and a material asset, called "test".

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
buxter
  • 583
  • 4
  • 14