How to create drag-and-droppable Interface references?

I’m trying to figure out how to use interfaces. This is my first attempt.

I would like to create an interface to have a panel which can display information from one of many different kinds of objects. I’ve been looking for a use case to learn interfaces with, and this seemed like an appropriate case.

Though I’ve gotten to the point of getting it to compile, I’m hitting a snag: interfaces don’t appear in the Inspector! I guess I shouldn’t have really expected it to in hindsight, but it does raise the question: how can I make a slot on my panel script into which I can drag an object of a class that implements the interface? Does the interface need some sort of attribute or need to inherit from something? And if it’s not possible to do this… then I’m getting the sudden impression that interfaces are nigh useless in the context of Unity.

        public ICameraInfo camInterface;
.....outside the class file....

    public interface ICameraInfo {
        float qualityLevel {get; set;}
        Camera myCamera {get;}
    }

You have to make the fields serializable, if not they will be hidden:

public interface ICameraInfo {
        [SerializeField]
        float qualityLevel;
        [SerializeField]
        Camera myCamera
       
        float QualityLevel {
              get {
                   return this.qualityLevel;
              }
              set {
                  this.qualityLevel = value;
             }
        }
        // ....
    }

@PolymorphiK_Games - not really… interfaces can’t have implementation in them. They’re an interface, all they do is define the interface a class might have. So you can relate classes together that aren’t necessarily in the same inheritance chain.

@OP - fields typed as interfaces aren’t serializable by Unity. So they don’t show up in the default inspector (unity only draws fields that can be serialized). Even if it did show up, it wouldn’t survive deserialization, so even a custom editor won’t work.

What I usually do is type my field as ‘Component’, and then write a PropertyDrawer and PropertyDrawerAttribute that defines what component types (interface or not) are allowed on that field.

1 Like

Ah yes, sorry sleep deprived…http://msdn.microsoft.com/en-us/library/ms173156.aspx

This the property drawer I use:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyBaseEditor/Inspectors/ComponentTypeRestrictionPropertyDrawer.cs

The attribute you flag with is ComponentTypeRestrictionAttribute found in here:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyBase/PropertyAttributes.cs

It’d be used like so in your code:

public class MyComponent : MonoBehaviour
{
    [ComponentTypeRestriction(typeof(IMyComponentInterface))]
    private Component _component;

    public IMyComponentInterface Component
    {
        get {return _component as IMyComponentInterface; }
        set { _component = value as Component; }
    }
}

The editor for this isn’t quite done though, the dropdown menu I use in it is a bit sloppy. Haven’t gotten around to updating it as it gets the job done for now.

So, 8 years later and this is still relevant, but in the meantime you switched to v4 of your framework, so I tried to find the equivalent scripts on the new repository.

I found these:

but the doc for class TypeRestrictionAttribute says:

which I found more below indeed.

Then I found the property drawers for the old one:

which uses UnityObjectDropDownWindowSelector.ObjectField with type check, and the new one:

where I’m not sure where the selection takes place… I see some “EditorGUI.Popup” but also usage of some “ChoiceSelector”.

Anyway, I suppose we should use [SelectableComponent(typeof(IMyInterface))] or something?

I haven’t tried it yet, maybe when I have more time I can clone the repository / add it via git url and try it out. I see it’s a personal repo like my own utils, so probably not stable enough to use any version, but if I can extract just the scripts I need it will be enough for me.

I wish I could help you out with this, but I’m swamped with work right now (trying to get approved by google play this week).

The reason that changed in version 4 is because I changed from just an object field that restricts what can be dragged onto it. Instead I now have a searchable window that pops up. That’s what the UnityObjectDropDownWindowSelector.ObjectField is.
8362434--1101252--upload_2022-8-14_17-27-44.png

And yes, you are correct in assuming that the library is generally considered unstable as it’s really just my own personal lib of tools for myself. I fix bugs as I trip over them in my own general use, or if someone points them out here on the forums. But I don’t really do full support as the responsibility of keeping an up to date stable build is far outside of what my schedule can allow.

Once we pass review, if you haven’t figured it out, I’ll come back here and try and update it with a stripped out version independent of the entire lib. But to be fair… I wouldn’t hold my breathe… I very well may just disappear after release for a vacation.