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?
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:
public class MenuItem
// Your content
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.