Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by madmonstor, Jan 8, 2018.

  1. madmonstor

    madmonstor

    Joined:
    Apr 29, 2015
    Posts:
    15
    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<GameObject>("Assets/AssetBundleSource/ItemTemplates/3DO.prefab"); // crashes

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

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
  3. madmonstor

    madmonstor

    Joined:
    Apr 29, 2015
    Posts:
    15
    I've tried all permutations and they all fail. Here are the results:

    These all crash the editor:

    var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("Assets/AssetBundleSource/ItemTemplates/3DO.prefab");
    var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("3DO.prefab");
    var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("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: []
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    I am using pretty much this for my assets from an assetbundle. I doing textassets, but this would be how it would be done.

    Code (CSharp):
    1. GameObject prefab = myLoadedAssetBundle.LoadAsset<GameObject>("3DO");
    If it's crashing the editor, it may be finding it, but something else might be going on...
     
    afeoktistov_penguin likes this.
  5. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
  6. terrycheung

    terrycheung

    Joined:
    Oct 9, 2014
    Posts:
    9
    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.
     
  7. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    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
     
  8. jalajshah

    jalajshah

    Joined:
    Mar 5, 2018
    Posts:
    60
    Hello Guys,

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

    gogoslab

    Joined:
    Jun 23, 2015
    Posts:
    1
    This is working for me:

    Code (CSharp):
    1. UnityEngine.Object gameObject = BundleManager.Shared.StoryBundle(story).LoadAsset(path, typeof(GameObject));
    2.  
    3.         prefab = ((GameObject)gameObject).GetComponent<ItemTemplate>();
     
  10. tamas_losonczi

    tamas_losonczi

    Joined:
    Jan 29, 2018
    Posts:
    2
    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":

    Code (CSharp):
    1.          
    2. var bundle = AssetBundle.LoadFromFile($"{Application.streamingAssetsPath}/modal");
    3. var names = bundle.GetAllAssetNames();
    4. var modalBundle = bundle.LoadAsset(names[0]);
    5.  
    Additionally, you can inspect the content of the Asset Bundle in the AssetBundle Browser: Screenshot 2020-12-08 at 12.45.42.png
     
    masoudarvishian likes this.
  11. masoudarvishian

    masoudarvishian

    Joined:
    Jan 15, 2015
    Posts:
    9
    It solved my problem, thanks.