1
\$\begingroup\$

In one of my custom C# classes in Unity, I need to expose a List of objects in the inspector, and they need to conform to a specific interface.

Below are two lines from my class definition. Card is a concrete type in my game, and CellObserver is an interface. Multiple classes may implement this interface, so I'd like to define it this way if possible.

public List<Card> currentCards;
public List<CellObserver> observers;

The list of Card objects shows up in the inspector just fine, but lists based on CellObserver don't. It's not unique to this interface either - I also have one on another class that behaves the same way.

The code builds just fine, and as far as I can tell, this is valid in C#. Is there something else I need to do in Unity to implement this list? Or is this simply not an option in Unity?

\$\endgroup\$
  • \$\begingroup\$ If your classes don't inherit from monobehaviour you can place [System.Serializable] above the class declaration line "public class Card {}" and that will show you basic variable types in the inspector. \$\endgroup\$ – Savlon Feb 15 '15 at 20:09
1
\$\begingroup\$

You'll probably have to look into PropertyDrawers to get what you want. These are custom property drawers for Unity. The Unity inspector doesn't know how to render a lot of things by default (and some things it's not very good at rendering, like arrays).

With a custom property drawer you can tell the Unity Inspector how to display your interfaces. Property drawers also allow you to change the way other types are displayed.

\$\endgroup\$
  • \$\begingroup\$ I created a course for Unity that covers these property drawers, it's a paid course, but you can use this 10-day free trial to view those videos and download the course material. \$\endgroup\$ – MichaelHouse Feb 15 '15 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.