2
\$\begingroup\$

So I have an ItemDatabase class which basically only has a list of Item

public class ItemDatabase : MonoBehaviour {
    public List<Item> Items = new List<Item>();
}

The Item class looks as follows:

[System.Serializable]
public class Item {

    public string Name;
    public int Id;
    public string Description;

    public Texture2D Icon;
}

Now I have some item types deriving from the Item class, for example Consumable

[System.Serializable]
public class Consumable : Item {
    public int Resore;
}

I next created a gameobject (empty) in my editor and hooked up my ItemDatabase to that object, providing a number of items in the list is no problem but I would like to make sure that - from my editor - I can choose a type of Item to put into the list, as of now, it only lets my add Item and not Consumable because it is a list of Item.

Can I also force the editor not to accept just Item by making it an abstract class?

EditorScreen

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Even though you're dealing with a different problem, I'd give near exactly the same answer as I did for this question. Basically, the Inspector is not that smart. You'll have to make your own property drawer for Unity to use instead of what the Inspector can come up with on its own. \$\endgroup\$
    – House
    Feb 17, 2015 at 15:16

1 Answer 1

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 it may render things in a way that's not suitable to you, or doesn't have the information you need.

Depending on the features you need, you might need to expand into a custom inspector. This is similar to a Property Drawer, but it's for the entire MonoBehaviour script instead of the specific object type. This is where modifying the inspector gets really powerful, since you can add all kinds of automation or other features to the Unity inspector. It's a great chance to optimize your workflow and make Unity more powerful.

\$\endgroup\$
1
  • \$\begingroup\$ I created a course for Unity that covers these property drawers and custom inspectors, it's a paid course, but you can use this 10-day free trial to view those videos and download the course material. \$\endgroup\$
    – House
    Feb 17, 2015 at 18:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .