MonoBehaviour that extends IDictionary<> shows up empty and throws an errors.

Issue #433 wontfix
Bjarke Elias created an issue

Copy-pasted from Discord.

I ran into something quite specific, but I wanted to mention it here in case it's a bug:

I have a SerializedMonoBehaviour with this field:

[BoxGroup("Status"),InlineEditor()]
public IListDictionary<int,T> CurrentObjects;

If I link another MonoBheavior that implements that interface, it works properly and is visible in the editor, however, if I remove the "InlineEditor" then linking the monobheaviour shows it as an empty dictionary in the inspector (even though it is not), and trying to add elements to it throws an Exception.

IListDictionary being an interface that extends IDictionary<int,List<T>>

Stack-trace: https://pastebin.com/DXUaHY54

Comments (4)

  1. Bjarke Elias reporter

    The stack-trace indicates something is going on with persistent-contexts? But the problem has most likely nothing to do with persistent contexts.

  2. William Besnard

    I can as well mention that the serialization happens properly in both cases, it seems that the bug is only on the visual side

  3. Tor Esa Vestergaard

    This is, in fact, intended behaviour - and hard to "fix" in the general case. The issue here is, as soon as the MonoBehaviour implements IDictionary<T>, then it is, as far as our system is concerned, actually a dictionary, and it will be treated as such. If exceptions are thrown when trying to add elements, this is likely because the MonoBehaviour's dictionary implementation is faulty somehow.

    My advice for this case is to create a special drawer for this type, with a higher priority than the regular dictionary drawer, and simply draw a regular Unity object field with it (if you want full Odin behaviour for it, refer to Odin's own UnityObjectDrawer.cs).

  4. Log in to comment