- Home /
View and Edit Properties of List items in inspector
I have a class called Menu. Menu has a List of MenuItems, each of which simply has two string variables: title and content.
I want to be able to define MenuItems right in the inspector while viewing an instance of Menu.
Right now I see this:
How can I make it so that I can expand each MenuItem in the list so that I can see its properties in the inspector, effectively creating new instances with the properties that I specify at runtime?
I'm thinking that I need to extend the editor to do this, but I am unfamiliar with editor scripting.
How would I go about implementing something like this?
What's the baseclass of $$anonymous$$enuItem? Can you include the class in your question?
ps: be careful with the term "properties" as there is something called property which is something different from a field / variable.
Answer by Bunny83 · Apr 28, 2015 at 02:43 PM
The screenshot looks a bit weird. Usually only classes that represent a serializable reference are viewed like this. So i guess you have derived your MenuItem class from ScriptableObject or MonoBehaviour. Either way both aren't suitable in this case.
You should simply declare it as an ordinary class without base class and put the Serializable attribute on it:
// C#
[System.Serializable]
public class MenuItem
{
// Your content
}
edit
Note: This way Unity doesn't support inheritance for the MenuItem class. In other words if you derive another class from MenuItem, technically you could store an instance in that array / List, but that's not supported by Unity. Unity serializes such classes based on the element type of the collection (array / List). So every item in the array / List has to be a MenuItem instance.
Is there a way to do this with inheritance? I need a list of different object defined by an abstract class.