Trying to load prefab from Assetbundle using LoadAsset always returns null.

I’ve built an assetbundle like this:

BuildPipeline.BuildAssetBundles(assetBundleDirectory, BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Switch);

Then I load the assetbundle like this from my streamingassets folder and it returns a valid assetbundle:

private AssetBundle myLoadedAssetBundle;
myLoadedAssetBundle = AssetBundle.LoadFromFile (path);

If I query the assetbundle for my prefab it finds it:

if (myLoadedAssetBundle.Contains(“Assets/AssetBundleSource/ItemTemplates/3DO.prefab”))
{
Debug.Log (“Found It”);
}

But if I try to load the assetbundle (of type ItemTemplate) it returns null or crashes. I’ve tried all the following:

ItemTemplate prefab = myLoadedAssetBundle.LoadAsset<ItemTemplate (“Assets/AssetBundleSource/ItemTemplates/3DO.prefab”); // returns null

ItemTemplate prefab = myLoadedAssetBundle.LoadAsset(“Assets/AssetBundleSource/ItemTemplates/3DO.prefab”, typeof(ItemTemplate)) as ItemTemplate; // returns null

var prefab = myLoadedAssetBundle.LoadAsset(“Assets/AssetBundleSource/ItemTemplates/3DO.prefab”); // crashes

The prefab contains a single component (a C# script).
Can someone shed some light? Thanks.

Please use code tags and you have already specified the path so try;

var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("3DO.prefab");

I’ve tried all permutations and they all fail. Here are the results:

These all crash the editor:

var prefab = myLoadedAssetBundle.LoadAsset(“Assets/AssetBundleSource/ItemTemplates/3DO.prefab”);
var prefab = myLoadedAssetBundle.LoadAsset(“3DO.prefab”);
var prefab = myLoadedAssetBundle.LoadAsset(“3DO”);

These all return null:

ItemTemplate ret = myLoadedAssetBundle.LoadAsset(“Assets/AssetBundleSource/ItemTemplates/3DO.prefab”, typeof(ItemTemplate)) as ItemTemplate;
ItemTemplate ret = myLoadedAssetBundle.LoadAsset(“3DO.prefab”, typeof(ItemTemplate)) as ItemTemplate;
ItemTemplate ret = myLoadedAssetBundle.LoadAsset(“3DO”, typeof(ItemTemplate)) as ItemTemplate;

Here’s the contents of the assetbundle manifest if that helps:

ManifestFileVersion: 0
CRC: 3414960933
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 602c0563b1f5994660a4cc8f1badb604
TypeTreeHash:
serializedVersion: 2
Hash: 9f32e4538e84957226458828f8b90421
HashAppended: 0
ClassTypes:

  • Class: 1
    Script: {instanceID: 0}
  • Class: 4
    Script: {instanceID: 0}
  • Class: 21
    Script: {instanceID: 0}
  • Class: 28
    Script: {instanceID: 0}
  • Class: 48
    Script: {instanceID: 0}
  • Class: 114
    Script: {fileID: 11500000, guid: 5db7b7329520f6d499e24ddf8fb25393, type: 3}
  • Class: 114
    Script: {fileID: 11500000, guid: 4d0c51bb0b6e93049af5e88f93826e3b, type: 3}
  • Class: 115
    Script: {instanceID: 0}
    Assets:
  • Assets/AssetBundleSource/ItemTemplates/3DO.prefab
    Dependencies: [ ]

I am using pretty much this for my assets from an assetbundle. I doing textassets, but this would be how it would be done.

GameObject prefab = myLoadedAssetBundle.LoadAsset<GameObject>("3DO");

If it’s crashing the editor, it may be finding it, but something else might be going on…

1 Like

I’m having the same problem suddenly. Any insights? @Brathnann @madmonstor

Dunno if this is the same issue, but I had this issue when having two assets with the same name but different extension. So for my case had i had “test.json” and “test.prefab” and it would still get confused even if you specified the type correctly. i just added a suffix to resolve it.

Hi,
I have the same problem right now. Anyone have found a solution ?
Unload and Load the bundle again solve the problem. Same behavior : Contains return true, load return null.

I really don’t understand what’s happening here ?

Thanks a lot

Hello Guys,

I had tried with prefix and only name but still no luck, any idea what is an issue ??

This is working for me:

UnityEngine.Object gameObject = BundleManager.Shared.StoryBundle(story).LoadAsset(path, typeof(GameObject));

        prefab = ((GameObject)gameObject).GetComponent<ItemTemplate>();

Hi all!
Have you tried calling GetAllAssetNames() to get the list of names packed in the AssetBundle? For example, the following loads the first item of the AssetBundle named “modal”:

var bundle = AssetBundle.LoadFromFile($"{Application.streamingAssetsPath}/modal");
var names = bundle.GetAllAssetNames();
var modalBundle = bundle.LoadAsset(names[0]);

Additionally, you can inspect the content of the Asset Bundle in the AssetBundle Browser:

1 Like

It solved my problem, thanks.