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. Dismiss Notice

Bug (WORKAROUND) AssetReference drawer disappears in Dictionary

Discussion in 'Addressables' started by CheeryLee, Jun 20, 2020.

  1. CheeryLee

    CheeryLee

    Joined:
    Feb 23, 2014
    Posts:
    16
    Hello everyone.

    I found a bug in Addressables 1.10.0. I use Odin Inspector & Serializer add-on for serializing Dictionary field in my script. Unfortunately when AssetReference is used as dictionary key or as a part of structure or class that is a dictionary itself, the drawer causes to fail: the field simply doesn't render in inspector, but I still can access it via right mouse button click.
    There is the very similar post on forum already: https://forum.unity.com/threads/assetreferencedrawer-does-not-dirty-the-serialziedproperty.568894

    I used a little workaround for that and I guess this post will be useful for everyone who faces that situation.

    There is a breaking piece of code:
    Code (CSharp):
    1. [System.Serializable]
    2. public struct A {
    3.       public AssetReference a;
    4. }
    5.  
    6. // ...
    7.  
    8. // it's really no difference between stock SerializeField and OdinSerialize in this case
    9. [OdinSerialize]
    10. private Dictionary<int, A> dict;
    11.  
    12. //...

    Снимок экрана 2020-06-20 в 14.44.23.png

    After a couple of minutes of debugging I found that serialized object has Sirenix.OdinInspector.EmittedUnityProperties.EmittedSOProperty_a_UnityEngine.AddressableAssets.AssetReference type, not like UnityEngine.AddressableAssets.AssetReference in regular structures.

    The problem part of code inside AssetReferenceDrawer.cs:
    Code (CSharp):
    1. public static T GetActualObjectForSerializedProperty<T>(this SerializedProperty property, FieldInfo field, ref string label)
    2.         {
    3.             try
    4.             {
    5.                 if (property == null || field == null)
    6.                     return default(T);
    7.                 var serializedObject = property.serializedObject;
    8.                 if (serializedObject == null)
    9.                 {
    10.                     return default(T);
    11.                 }
    12.  
    13.                 var targetObject = serializedObject.targetObject;
    14.                 // targetObject has been serialized differently
    15.  
    16. // ...

    At the end of the try-catch block it tries to get value from FieldInfo, that's wrong for dictionary serialized field:
    Code (CSharp):
    1. // ...
    2.                 var obj = field.GetValue(targetObject); // causes a fail and goes to catch it, returns default class instead
    3.                 return (T)obj;
    4. //...

    The result of everything above is a null value in m_AssetRefObject variable. When it's null, the field won't be rendered in inspector.

    I used a fast workaround for this situation:
    Code (CSharp):
    1.         public static T GetActualObjectForSerializedProperty<T>(this SerializedProperty property, FieldInfo field, ref string label)
    2.         {
    3.             try
    4.             {
    5.                 if (property == null || field == null)
    6.                     return default(T);
    7.                 var serializedObject = property.serializedObject;
    8.                 if (serializedObject == null)
    9.                 {
    10.                     return default(T);
    11.                 }
    12.  
    13.                 var targetObject = serializedObject.targetObject;
    14.  
    15.                 // add another one condition for descending a hierarchy
    16.                 // (just check the type of target object: is it not a AssetReference itself)
    17.                 if (property.depth > 0 || targetObject.GetType() != typeof(T))
    18.                 {
    19.  
    20. // ...

    After such manipulations the problem is gone.

    Снимок экрана 2020-06-20 в 15.04.09.png

    This solution has a con: if you use an array of AssetReference, there will be $0 instead of a nice variable name label. Probably it should be fixed inside DescendHierarchy method.
     
    diesoftgames likes this.
  2. CheeryLee

    CheeryLee

    Joined:
    Feb 23, 2014
    Posts:
    16
    Bump for developers of Addressables.
     
  3. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,796
  4. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,796
    If you haven't, can you upgrade to version 1.11.2? That might resolve this.
     
  5. CheeryLee

    CheeryLee

    Joined:
    Feb 23, 2014
    Posts:
    16
    Hi! I was a bit busy and haven't filed a bug report. Just tried a new version you mentioned. The bug is still here. Look at the picture inside a spoiler.

    Also I posted a gist with workaround changes in version 1.10.0. You can diff it with vanilla one:
    https://gist.github.com/CheeryLee/ac726bd688ec0abdafcd6af05036b25f

    screen.png

    screen1.png
     
    Last edited: Jul 1, 2020
  6. CheeryLee

    CheeryLee

    Joined:
    Feb 23, 2014
    Posts:
    16
    I'm here to say that the bug is still presented in 1.13.1 in Unity 2019.4.6.
    My workaround is still much helpful. It's a bit strange why you don't want to use it to close the error.
     
    Last edited: Aug 15, 2020
  7. joel_unity689

    joel_unity689

    Joined:
    Nov 28, 2020
    Posts:
    5
    I'm also experiencing this issue. CheeryLee's workaround solves it for me - but I can only figure out how to apply it by editing the code in my Library/PackageCache folder, which doesn't seem like a great solution (as it keeps getting invalidated and deleted). Is there a correct place to apply this workaround, CheeryLee?

    EDIT: After some further googling, I simply copied the addressables package from Library/PackageCache to <MyProject>/Packages.
     
    Last edited: Feb 21, 2021
  8. CheeryLee

    CheeryLee

    Joined:
    Feb 23, 2014
    Posts:
    16
    It's definitely in Unity's package cache in AppData, if you are using Windows.
     
  9. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    114
    This is appears to still a problem in 1.16.10.
     
  10. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,796
    As this still appears to be persistent in more recent versions, please file bug reports so that we can dig into this further. :)
     
  11. saferusmail

    saferusmail

    Joined:
    Jul 24, 2020
    Posts:
    2
    Ticket created (but the Unity team tells me not to share the error url).
     
  12. joel_unity689

    joel_unity689

    Joined:
    Nov 28, 2020
    Posts:
    5
    Still present in 1.18.11
     
  13. Qusdrok

    Qusdrok

    Joined:
    Jun 28, 2020
    Posts:
    24
    they still haven't fixed it in version 1.18.19
     
  14. zachlindblad

    zachlindblad

    Joined:
    Sep 29, 2016
    Posts:
    39
  15. Davon92

    Davon92

    Joined:
    Oct 24, 2014
    Posts:
    17
    this is a huge bummer
     
    MrDizzle26 likes this.
  16. MrDizzle26

    MrDizzle26

    Joined:
    Feb 8, 2015
    Posts:
    34
    We'd love to see this fixed, we use custom editors and Odin to improve out workflows, this limitation is extremely inconvenient and nullifies much of the utility of our custom inspectors.