Search Unity

Question Is it possible to get asset bundle name from the prefab.

Discussion in 'Asset Bundles' started by Gunner22, Mar 13, 2023.

  1. Gunner22

    Gunner22

    Joined:
    Jan 15, 2020
    Posts:
    56
    I have a prefab in my scene that is part of an asset bundle. There is a manager script that instantiates my assets in an empty scene. It know what asset bundle to instantiate based on the json file. I want to prepare this json file with an editor script so I was wondering is there a way to get asset bundle name directly from the prefab.
     
  2. AndrewSkow

    AndrewSkow

    Unity Technologies

    Joined:
    Nov 17, 2020
    Posts:
    91
    The loaded prefab itself is not aware of the AssetBundle that it comes from, and I'm not aware of a way to get the AssetBundle name from it.

    If you compile your AssetBundles with BuildPipeline.BuildAssetBundles() then there are .manifest files generated for each AssetBundle. This is a yaml format file that has a list of Assets inside it. So one approach for generating your JSON might be to process those files to produce a table of asset path -> AssetBundle mappings.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    @AndrewSkow The AssetBundle.Unload API allows unloading an asset bundle, and optionally unloading all objects that were loaded from it.

    I wonder how this is done, if there's no link between loaded objects and the bundle they were loaded from.
     
  4. AndrewSkow

    AndrewSkow

    Unity Technologies

    Joined:
    Nov 17, 2020
    Posts:
    91
    That is a good question.

    Inside AssetBundles there are one or more SerializedFiles. Part of the work of loading an AssetBundle is to update some data structures with a mapping between object instance ids and the SerializedFile they are persisted in. When an AssetBundle is unloaded, we have the ability to find all the SerializedFiles loaded from that AssetBundle, and use those data structures to find all the objects associated with them. That lets us destroy them.

    However these structures don't offer a way of finding the AssetBundle name from an object. The SerializeFiles inside the AssetBundle have internal names like "CAB-21d21e204bc82a3395d1885717985b77". From an object it would be possible to find that name, at least using internal APIs. But I'm not aware of data-structures that would aid in tracing back from the SerializedFile name to the name of the AssetBundle. So thats why i had suggested building a map based on the assets and assetbundle information in the .manifest files.

    Having that ability does seem like an interesting idea so we will keep it in mind!
     
    liortal likes this.