Search Unity

Bug Scenes are duplicating/unloading assets although everything is marked as addressables

Discussion in 'Addressables' started by Xarbrough, Jul 1, 2020.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Maybe I'm misunderstanding the documentation, but I'm running into a strange issue:

    There are two scenes A and B. Both scenes are marked as addressable and placed in different groups. They are loaded from an almost empty bootstrap scene. Both scenes directly reference asset C via an object field.

    According to the documentation, this will duplicate asset C in memory, since the scenes are placed in different bundles and each bundle will pull in the direct reference, hence copying it.

    To solve this, I marked the asset as addressable and put it in a new group D. As I understand it, this should solve the duplication since now both scenes can point to the shared reference of asset C within bundle D. This is also exactly what the Analyze tool detects as an issue and provides a fix for by putting the asset into the "Duplicate Asset Isolation" group.

    However, in my case, asset C is still duplicated in memory, which is a really big issue, since the asset must be unique for logic reasons. The same issue happens with e.g. Audio Mixers, where it is very important to have the correct instance.

    Please correct me if I'm totally misunderstanding how Addressables works, but to me, this looks like a dependency bug.
     
  2. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
    I'll flag this for the team to share some guidance - which version of Addressables are you using?
     
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Thank you! We're on 1.10.0 and Unity 2019.4.0f1. I'm going to try the update to 1.11.2 now.
     
  4. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I've submitted case 1260183.

    Here's what I found: My assumptions about how Addressables work seem correct for simple setups, e.g. two scenes directly reference the same asset and they are loaded one after the other. Addressables Analyze tool will warn about any duplicated assets and the fix to put these assets into a seperate group works fine.

    However: In some cases, Addressables unloads assets although they are referenced. For example, when starting in Scene A, then moving the script to DontDestroyOnLoad, then unloading Scene A, the reference to the asset is lost. I've tested some more and found that the issue still happens when I use a custom DontDestroy scene and manually move the GameObject to it.

    To me, this looks very much like a bug, but I'm also unsure if direct references are even supported when using Addressables. @TreyK-47 Would it be possible to confirm with the team whether my project should try to completely remove all direct references or if we can try to find a different workaround? Since we're close to our deadline, it would take significant effort to change all direct references.
     
    Last edited: Jul 2, 2020
  5. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,822
    Thanks for the follow-up, and additional details. I'll be sure to pass along any guidance the team has, or they may respond as well. :)
     
  6. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Hey @Xarbrough so just to confirm direct references (and just to confirm you mean you have a public GameObject field that you're assigning to, correct? Not a public AssetReference field? or perhaps you mean the object is already part of the scene?) are not really supported when using Addressables. That is to say you can definitely use them in your project but they won't have any affect on the reference counting happening behind the scene. As for your original post, I'd be curious to see what's going on with those direct references. It may be we aren't able to support that case given a potential scene dependency issue, or a bug in the Analyze logic, or a bug outright.

    To your second post, given that direct objects in a scene have no weight as far as reference counts go, marking an object as DontDestroyOnLoad will lose all of its references when the scene that contained that object is unloaded.

    Say you have SceneA with PrefabA. The asset bundle containing SceneA is loaded when you load SceneA and thus has a ref count of 1. This asset bundle contains all the data for your PrefabA. You move PrefabA to DontDestroyOnLoad (which basically just moves it to a new scene). Then you load SceneB (or unload SceneA in some way) so SceneA is unloaded, its ref count goes to 0 and the asset bundle is unloaded. The Object you have marked as DontDestroyOnLoad has not way to keep that bundle loaded so all of its data gets ripped out from beneath it.

    Two possible solutions:
    1) Call Addressables.ResourceManager.Acquire(....) and pass in the scene handle you get from loading SceneA. This will bump the ref count which will keep the bundle loaded even when you unload the scene. Do keep in mind that you'll have to manually release that handle whenever, if ever, you're done with that object that you marked DontDestroyOnLoad.

    2) Make the object you would normally mark DontDestroyOnLoad an Addressable asset. Then have a script that loads it as part of SceneA and moves it to DontDestroyOnLoad. This will give that object its own reference count and will keep its data loaded until you destroy/release the instance yourself.

    I hope some of that helps!
     
    Xarbrough likes this.
  7. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Yes, it's a
    public GameObject myPrefab;
    or rather
    public AudioMixer mainMixer;
    . The referenced assets are not directly part of the scene, they are project assets.

    Thank you for explaining the two possible solutions, both work for me! In the meantime I've changed all direct references to Addressables AssetReference, but ResourceManager.Acquire also looks promising, didn't know about that one.