Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Direct reference within asset bundle

Discussion in 'Addressables' started by NamelessPerson, May 13, 2020.

  1. NamelessPerson

    NamelessPerson

    Joined:
    Apr 17, 2017
    Posts:
    26
    I have a scriptable object for a video file that contains references to files needed to play the clip (i.e. the VideoClip, the Subtitles, a name for the video). It is inside a Adressable group bundled with those referenced files and will be loaded asynchronously with LoadAsset. However I'm curious, what happens if the file references WITHIN the SO are direct references?

    So flow would be:

    Video Loader is passed SO Reference -> Loads Asset through addressables -> parses referenced subtitle file

    Or would I need to also load the subtitle file asynchronously?
     
  2. CameronND

    CameronND

    Joined:
    Oct 2, 2018
    Posts:
    88
    The addressables system will take care of it for you. The catalog contains information about which assets depend on eachother so when you load one asset it should also load all its dependencies too.

    One thing to note: Any asset that is a direct reference from another asset will be pulled into an asset bundle during the build process if its not already marked as addressable. This is where you can end up with duplicates. If the directly referenced asset is already set up in an addressable group then you dont have to worry about that.
     
  3. NamelessPerson

    NamelessPerson

    Joined:
    Apr 17, 2017
    Posts:
    26
    Thanks! I was able to figure this out after a bit of testing but I still appreciate the response.
     
  4. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    Are you sure? I have been having issues where I will unload an asset and something that is depending on by something else loaded by a bundle loses it's reference. This happens with materials, scriptable objects, etc. Everything that is loaded in my game except for the bootstrap scene has been marked addressable. Even every dependency has been marked this way (to avoid asset duplication).
     
  5. NamelessPerson

    NamelessPerson

    Joined:
    Apr 17, 2017
    Posts:
    26
    Yes I am sure but I would suggest being very careful. In my use case I had a group for assets together in one bundle (I.e. a VideoClip, thumbnail sprite, and subtitles). I then also had a ScriptableObject called VideoInfo in that bundle with direct references to those files. I LoadAssetAsync() on the VideoInfo and it correctly loads and uses the direct references to the other files and unloads them when I am done and use Release().

    In your instance it sounds like you have direct references ACROSS bundles. When using Release() I believe Addressables will also unload the asset bundle if no more Addressable References exist and not take into account if Direct References still exist which may be the cause of your issue.
     
  6. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    It does indeed sound like it... but then that makes the use of the Duplicate Asset analyzer completely broken in some cases. I don't use that but I do a similar thing. Having to implement addressables everywhere that points to something addressable is too much to say the least. It seems like this shouldn't be an issue if they're calculating dependencies correctly, wouldn't you agree?
     
    andreiagmu likes this.
  7. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    I have figured out why. The issue is that Addressable seem to work with dependencies at the object level.

    Object A loads from bundle ONE
    Object B loads from bundle ONE and depends on Object C
    Object C necessarily loads from bundle TWO

    Object B is no longer needed and is released (but stays in memory because you can only unload objects by unloading a bundle... this has always been a rule)
    Object C unloads and unloads bundle TWO with it because it's no longer required

    Object B is requested again and "loads" from bundle ONE... but the object is already loaded so it just gives you the object again.
    Object C that is referenced in Object B is now NULL because it was unloaded in the previous step.

    Unity needs to change the addressables to coalesce the dependencies when a new object is loaded. Those become a reference count on the new bundle once that happens.

    @unity_bill Is this a correct assessment? Are there any plans to fix this?
     
    andreiagmu likes this.
  8. NamelessPerson

    NamelessPerson

    Joined:
    Apr 17, 2017
    Posts:
    26
    hmm... I haven't used the Duplicate Asset analyzer so I am not sure but I don't think I quite agree. Though I think the main issue is a huge lack of documentation and detailed approaches. If you want to use addressable you have to change your way of thinking of assets.

    In my use case I have implemented addressables everywhere that references are needed across bundles. I made a wrapper for sprites and text ui that will load assets asynchronously during runtime but also display them in the editor window so I can build prefabs. The downside to this is that a prefab may load slightly ahead of an image inside it but for me that hasn't been a noticeable issue.

    Are you only using LoadAssetAsync/InstantiateAsync and ReleaseInstance/Release ? From my understanding and usage what you described is not how it works.

    Additionally from a design standpoint is sounds like you should adjust your bundle layout. If A & B do not depend on each other and B & C do it sounds like your bundles should be setup as Bundle 1 - (A), Bundle 2 - (B,C).
     
  9. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    Relying on a bundle layout to determine if a bug will occur or not does not seem like a good idea. Also, the more complex your game becomes so do the bundles and dependencies between them - making finding these issues near impossible. The solution is that everything that is addressable needs to be referenced that way. Which is itself a nightmare.

    It means that you can't effectively design your data very well as it requires asynchronously loading many fields in every prefab / asset that gets loaded. I actually have a number of assets that do exactly this and only load their required objects on demand - a second ref count on top of Unity's. In this case though I'm not using that. The object that is being destroyed is being done so through a scene load which Unity handles and unloads the objects from addressables correctly for.

    The issue really occurs because Unity cannot unload objects that are not referenced but are from a bundle. If that simple thing were fixed (I'm not sure that it can be?) then this problem would actually go away and things would work perfectly.
     
    andreiagmu likes this.
  10. adani_zynga

    adani_zynga

    Joined:
    Oct 6, 2022
    Posts:
    6
    I am currently facing similar issues where direct references to addressable assets across bundles are NULL. The dependency chain itself if broken after building the content update. @KAJed are you still facing this issue?
     
  11. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    I haven't run in to this issues recently so I can't confirm nor deny if it has changed.