Search Unity

Why doesn't Unity directly support interfaces in Inspectors

Discussion in 'Editor & General Support' started by JeffersonTD, Dec 13, 2018.

  1. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    I know you can do a workaround for showing interfaces in inspectors by making custom inspectors, and I've done that in a few inspectors, but especially if that's the only reason for a custom inspector, it doesn't feel right.

    So the question is: why is it exactly that Unity has never created support for interfaces in Inspectors? What's the technical reason behind that? And is it likely that support will at some point be implemented?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    I don't know their reason, but Interfaces use Properties for variables which could have any Get/Set code on the implementation side. I imagine it is a complete nightmare to support Properties sufficiently and in a stable way and thus there is no reason to support Interfaces.
     
  3. Inspector is working through serialization.

    If I remember correctly it is because properties are behaviors, the serialization does serialize data, but not behaviors, because it's impossible to do so. You can serialize behaviors by adding custom getter methods and you need to inherit from the ISerializeable interface. Which hinders any generic attempt to create a serializer which runs on any data.
    https://docs.microsoft.com/en-us/do...ization.iserializable?view=netframework-4.7.2

    Although I may be wrong, it was a long time ago when I looked up such a thing.
     
  4. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    What do you mean by behaviours in this context? MonoBehaviour references at least are as serializable as anything.

    The funny thing just is that the at least in this case, any of the things that implement the interface can be serialized. But well, I have a workaround that doesn't cause much additional effort, so this is more about seeking an understanding.
     
  5. I'm not talking about MonoBehaviours. I'm talking about the behaviors as in C# context. It can contain custom code, like properties.
    You can't serialize properties, not even in MonoBehaviours, you need the underlying variable and that can be serialized (or write custom serialization like in the example) and you need to make sure that after deserialization whatever side effects you do upon property set you do.

    Visit the link I put in there, if you get an overview, it becomes more understandable.
     
  6. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    Thanks. I did check the link, but couldn't still get to the point. I mean sure, properties can't be serialized, but they can exist in MonoBehaviours and in interfaces, but only the first can be serialized, so I can't see why that would be a reason why fields that have the type of an interface couldn't be serialized. And in general I'm not concerned with serializing properties: if I need to do that, then I'll just use a custom inspector.

    Is the reason you are talking about properties the fact that interfaces can't contain fields? In any case, I'm not talking about serializing the contents of the interface or anything that implements it, but just simply serializing a reference to a script.
     
  7. Then you're good to go. :D It wasn't clear to me, and yes, this was the reason.
     
  8. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    Ok. :) Well, then my actual question still remains without an answer. Would be interesting to hear a comment from eg. someone from Unity.
     
  9. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    945
    Not an answer to the question, but odin inspector supports the usage/serialization of interfaces in the inspector.
     
    JeffersonTD and budukratok like this.
  10. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    Thanks for the tip! It seems to be using Attributes for the cause and that has been my principle as well. I never thought of directly being able to serialize properties though (I've just done one manual work around in one case), but apparently Odin would do that too. I wonder how it does that... the problem is that for hobbyist stuff the threshold for spending money for assets is much higher.

    Also: I wonder how well it works with the new Unity prefab flow. Since the new prefab system seems to so far still have some issues by itself also, I'd assume Odin doesn't directly work that perfectly with the new prefab system either. Of course interesting to hear, if any one has any experience of that.