Search Unity

Resources references in addressable scenes creates copies of object instead

Discussion in 'Addressables' started by Mitjans_AmuseNetwork, Aug 1, 2019.

  1. Mitjans_AmuseNetwork

    Mitjans_AmuseNetwork

    Joined:
    Aug 3, 2017
    Posts:
    24
    We integrated a "modular system" to our project - using scriptable objects to store and inject data between behaviours and scenes (https://unity3d.com/how-to/architect-with-scriptable-objects for more info).

    Normally, when switching scenes, the "modular variables" keep their data from past modifications. Also, when integrating the addressables system and to make sure all data is included in build, we stocked all the scriptable objects into the resources folder.

    The problem comes when we add references of these objects into a scene included in an asset group (local or remote) and we change the values. It looks like Unity is creating a copy of the scriptable object taken from the resources, so when we switch back to another scene, all the changed data has been lost (the copy is destroy and the modifications are not applied to the original resource).

    I created a small workaround that looks for the original variable in the resources folder and returns it if found, but it is pretty tedious as I also have to save the resource path into the scriptable object and call this function into the Start() method or similar everytime there is a public reference of the object in a script.

    Code (CSharp):
    1.     public ModularVariable GetFromResources()
    2.     {
    3.         ModularVariable instance = this;
    4.         //loading real resource object:
    5.         var resourcePath = this.ResourcePath;
    6.         if (!string.IsNullOrEmpty(resourcePath))
    7.         {
    8.             var resource = Resources.Load<ModularObject<T>>(resourcePath);
    9.             if (resource) instance = resource;
    10.             else Debug.LogError($"{name} not found - path: {resourcePath}");
    11.         }
    12.         return instance;
    13.     }

    I have added a small project showing the problem. In the project: if the "Circle_Color" or the "Text_String" saved in resources are modified in the "Builded_Scene", we can that the data is kept (also in editor). If changed in the "Addressable_Scene", the data is deleted with the scene. Use "Pack Play Mode" with the addressables built to see this behaviour. In "Play Virtual Mode" or "Play Fast Mode" (not using the real bundles), the project works as it should.

    I am using Unity 2019.1.10 and Addressables 1.1.5.

    Thank you very much!
     

    Attached Files:

    glitchers likes this.
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    I took a quick look at your project, and I think this context will help you...

    Addressables provides the ability to load things from Resources, or load things that you have marked as Addressable. When you mark something as addressable, we build it into an AssetBundle.

    If you have a direct reference to something (such as "public ColorVariable color;"), then Unity does the loading based on how it understands the dependencies.

    While we've built a wrapper around Resources and AssetBundles to allow us to load something from one or the other, Unity does not support the two systems talking to each other. So, when we build your scene into an AssetBundle, Unity sees that this scene has a direct reference to a ScriptableObject, and pulls that in as a dependency. Unity duplicates the SO and puts a copy into the AssetBundle. So at load time, if you load the scene, you get the copy from the bundle, and if you load the SO directly, you get the copy in Resources.

    If you move the SO out of resources, and mark it as addressable, it'll end up in a bundle. Now, Unity will understand that the addressable SO is in a bundle, so it doesn't need to duplicate it. Now, even if they are in separate bundles, loading the scene will load the same SO as loading it directly. We have seen some occasional bugs surrounding data reset in prefabs, so if you change your SO to be addressable, and still have data getting messed up, let us know.
     
  3. Mitjans_AmuseNetwork

    Mitjans_AmuseNetwork

    Joined:
    Aug 3, 2017
    Posts:
    24
    Hello, Bill, and thank you for your reply!

    The thing is that when we implemented addressables, we moved all SO into different addressable groups, with scenes in it. In editor fast mode and editor virtual mode everything worked as expected, but in build mode (editor and actual build - for Android platform) some SO references inside the scenes were lost, thus breaking the game. It looks like the image in the editor (first image, editor - second in play mode from editor Builded Mode for Android)


    bug_loosing_ref.jpg



    For info: We have some built scenes in the game that have direct references to bundled prefabs and SO (and prefabs with SO in it). And in the same bundle we have some extra scenes that also include similar references. The project is quite big to send :(

    I observed then that placing those SO to the resources folder (they are pieces of small data so it's ok the save them there) the bug disappeared, but then there was the problem with duplicated SO in bundles and game.

    I have not been able to replay the issue of the lost reference in the stripped project, but placing SO and prefabs to an asset group and then adding them into a builded scene have the same issue duplicating the assets.

    I attach to you the stripped project with the SO in an addressable group with some prefabs containing references to them. It uses different copies of SO betweeen builded scenes and scenes from an AB.

    Thank you very much again,

    Joan
     

    Attached Files:

  4. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    So let me see if I can summarize...
    1. the duplicates are due to the addressabes/resources interactions I mentioned in my reply.
    2. if you don't put SO's in Resources you sometimes have data lost.
    3. The attached project does not reproduce #2. Or it does?

    I'm very interested in the lost data bug. If the attached project does repro it, please tell me how. If not, then I'm not exactly sure what I'm supposed to be looking at in this new project.

    One thing to check on if this problem is intermittent is what your build target is set to. If you set the editor build target to Android, then build bundles and run in PackedMode, it'll build and load bundles intended for use on the Android platform. But your editor is running in windows or mac. This often leads to weird errors. Just something to check.
     
  5. Mitjans_AmuseNetwork

    Mitjans_AmuseNetwork

    Joined:
    Aug 3, 2017
    Posts:
    24
    That is exactly how you say (couldn't reproduce the lost data bug in the sent project) .I know that switching to an "android build target" might lead to some errors, but in a build a have the same lost data bug, tested in an android device.

    I will try to send you a cut version of the bugged project during the day so you can check it. thanks again!
     
    unity_bill likes this.
  6. Mitjans_AmuseNetwork

    Mitjans_AmuseNetwork

    Joined:
    Aug 3, 2017
    Posts:
    24
    Hello Bill!

    Sorry, yesterday I couldn't finish to send you the project. The project I send you loses all the reference to "ColorVariable" when building player, at least in my case. I have been running some tests and I found the following:

    - I deleted most of the project assets and then started to reimport scripts and assets (keeping the same .meta files an hierarchy) to check if/when the project started to lose references when building player.

    - At one point, and after building player content, it looks like the AddressableAssetSettings automatically removed the "built in data" group from the groups list (it wasn't there). I thought at first that It could have been me who removed it by mistake, but I checked that when you delete a group from the Addressables Window, Unity also deletes the AssetGroup asset (even the 'built in' one) and in this case the asset was still in the project hierarchy (just not in the groups list).

    - I don't know if it is related, but I observed that at that moment I had some prefabs with "missing scripts" and materials with "missing shader" in the resources folder.

    - To retrieve the 'built in data' group, I removed the "AddressableAssetsData" folder and generated a new one from the Addressables window (thus it readded the "built in group"). When I rebuilded the addressables again, at first it seemed that everything was working again. But after tweaking some scenes and prefabs values, the lost references bug reappeared (in my case ColorVariable SO in addressables and in scenes not added in build). At another point after cleaning all data builders, the built project couldn't find the ColorVariable SO from resources, but it found the ones in the asset bundles.

    - After that (in with a pretty broken project!), Unity started sending me these warnings when loading a scene:

    missing_script.PNG

    - I checked that the scenes don't contain any 'missing scripts' and I also checked that serialized fields in scripts are included outside the editor. - A while ago I saw this same warning and at the time I checked the '*.unityscene' with a text editor, I think that there were some guids of scripts that didn't exist anymore in some gameobject (but without the gameObject showing the "missing script" component in the editor) - reading a scene file is not easy for me so i cannot be sure either.

    Some other notes:

    -I have tried to "reimport all" / delete the library and reset unity, both methods don't seem to affect to the lost references bug.


    -If I load the an assets in runtime that was lost in a scene reference (in my case all assets of ColorVariable type), the system finds them without any problem.


    -In our project we are using the deprecated package "SVGImporter" from Jaroslav Stehlik (we will try to switch to the native new SVG importer in the future). When building without this package, everything worked fine, but when I reimported it, the bug reappeared.
    -> after that, I removed the package again and rebuilded to check if the player worked properly again, but the bug stayed.

    I hope this helps and thanks again, Bill!

    (unity version = 2019.1.1 / addressables 1.1.7)
     

    Attached Files:

  7. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    I've pulled down your project, and there's a lot going on there, so I'm not sure what exactly the repro steps are.

    But, based on the missing script thing, I think I might know what's going on. There is a scenario that causes the loading of assets to not understand which reference to a script to use. See my explanation in this thread: https://forum.unity.com/threads/the...his-behaviour-is-missing.652861/#post-4597216

    As I note in the thread, the problem "fixes itself", but I wouldn't be surprised if that also clears out the data. This was an engine problem. We've got it fixed, but I don't believe that fix is out the door yet. The fix should either be in 2019.3.0a12 if that exists, or the first 2019.3 beta.

    So once that's released, please try your project there and see if that fixes the problem. Also, if you feel inclined, feel free to give some clear repro steps for me to try with the attached project.
     
  8. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    Do note that the scenario actually occurs in scenarios that are supposed to work according to that thread.

    It doesn't really fix itself as such, as the fixed references get the wrong type a lot of the time, dropping back to Object, as found in https://forum.unity.com/threads/case-1174117-addressable-dependency-resolution-is-unreliable.721658/

    This is serious enough that it must be backported pretty much everywhere.
     
  9. Mitjans_AmuseNetwork

    Mitjans_AmuseNetwork

    Joined:
    Aug 3, 2017
    Posts:
    24
    Sorry for not explaining properly! I tried to send a cleaner project, but if I start cutting more prefabs and scripts, the addressable system builds without any errors.


    Steps:

    1 - Build Player Content (i did it in windows target)
    2 - Play Mode Script set to Play Packed Mode
    3 - Open the "ScenesManager" scene, enter play mode.
    4 - Click play button to load next scene, and from that scene either click "load map 1" or "load map 2".
    5 - In next scene, check the logs (some direct references are lost)

    Everything is placed in the same group bundle (using folders in the bundle, set to 'Pack Together'), though some prefabs have references to objects in the resources folder.


    In the version I sent, though, I saw afterwards that there are several materials in 'Global/Resources/Materials' folder that use a "heart" texture coming from the bundle group, which I dont know if it might lead to any bug. -> it could be interesting to send a warning when this happens, though. Placing the texture to the resources folder and building afterwards didn´t solve the bug.

    Thanks!
     
  10. HugoClip

    HugoClip

    Joined:
    Feb 28, 2018
    Posts:
    52
    Will this fix be backported to 2018.4 LTS?
     
  11. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    According to Ryan it’s being backported to 2018.4 and 2019.2.
     
    unity_bill likes this.
  12. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    using those steps I was able to see the logs of "whatever exists: false" in 2019.1, but "whatever exists: true" in 2019.3.0a12. I'd recommend updating to that.
     
  13. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    It has now landed in 2019.2.3f1!
     
    glitchers and unity_bill like this.
  14. KAJed

    KAJed

    Joined:
    Mar 15, 2013
    Posts:
    122
    How does one use the Addressables interface to load an object from Resources? They cannot be assigned to an AssetReference as they don't seem to have a valid key.
     
  15. simbaDraco

    simbaDraco

    Joined:
    Oct 16, 2014
    Posts:
    18
    @unity_bill I created 2 addressable scenes in a content project and downloaded them in the main porject using loadcontentcatalog and I am getting missing scripts. Creating addressable scenes in main project is working fine.
    Any Ideas?
     
  16. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    Hello , I searched but I didn't find out how can I load assets from resource via Addressable ? Thanks .
     
  17. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,820
    I'll ping the team for some guidance!
     
  18. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    Hey all, so in regards to the "How do I load assets from Resources that are in Addressables?" Here's what you do:
    1) add your asset to Resources like normal, lets say you have something like Resources/myprefab.prefab
    2) Make sure the Built In Data Addressable group has "Include Resources Folders" checked (it should be by default)
    3) Pass the same value you'd give to Resources to load myprefab to Addressables (in this case just "myprefab" but you'd need to include the folder structure under Resources if you had it, same as using the Resources API.)
    4) Use loaded asset as desired
     
  19. davidla_unity

    davidla_unity

    Unity Technologies

    Joined:
    Nov 17, 2016
    Posts:
    763
    @simbaDraco Sounds like something got stripped in the build of your secondary project. I'd try to load those scenes in the project they were created in and see if the same thing happens. Also double check all your AssetBundles are in the correct locations. If you're still having issues you could try re-building your AssetBundles just to make sure (doing a clean build if possible). Also you may want to start a new thread for this.
     
  20. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    137
    @davidla_unity
    @TreyK-47
    @unity_bill

    I have read this thread from top to bottom but I'm still hesitated. So, I have ScriptableObject called A that have reference ScriptableObject called B. Inside B, there is a gameobject called X that don't have addressable. Then, I make A and B both addressable. Does the gameobject X will be duplicated on build?

    A -> B -> X

    Edit:
    I have found The Addressables Analyze tool just now, and it does show the gameobject X duplicated. The Addressables Analyze tool can also fix the duplicate gameobject too. Really nice.
     
    Last edited: Jan 14, 2023