- Home /
Inspector: custom property with custom type use default editor
It's hard to give a title for this question, sorry for this.
My situation is this: I have a script that should set a property called "Monsters" (a list) on a given gameobject. This list is composed by a lot of Spawnable, wich is a simple class built in this way:
public class Spawnable : MonoBehaviour
{
public uint Amount;
public bool Respawn;
public GameObject Monster;
}
So the Spawner script that is attached to my "Spawn" empty gameobject (a prefab):
public class Spawner : MonoBehaviour
{
public List<Spawnable> Monsters;
}
Are there any possibility to do this by allowing unity to automatically generate the editor for my custom type with its base editors? (one for int, one for bool and one for gameobject). Are there any way to set everything I've shown in the inspector?
Thanks for any suggestion I would like to edit through the inspector my monsters list by setting its size (this is already possible) and set Spawnable properties for each item.
Answer by syclamoth · Jan 11, 2012 at 09:55 PM
Yes, it's very straightforward-
Your 'simple class' shouldn't be descended from MonoBehaviour. The MonoBehaviour class is a complex class with lots of functionality, and it's not really intended to work that way. Instead, just use the 'System.Serializable' attribute, and make it a base class.
[System.Serializable]
public class Spawnable
{
public uint Amount;
public bool Respawn;
public GameObject Monster;
}
Then, when you create your public list, it will give you a sizable dropdown-list with the correct properties for the type.
Correct! Data class shouldn't inherit from $$anonymous$$onoBehaviour. They should be made as base class or you may want them to inherit from Object or ScriptableObject, which are explained in the Scripting Reference.
Your example is great and works really way, thanks a lot! (also to you Byologic). I used the $$anonymous$$onoBehaviour only because it was the only way I know to make something appear in the inspector, I'm more happy with this approach keeping inheritance for other important things :)
Answer by by0log1c · Jan 11, 2012 at 09:59 PM
If you are not using custom inspector script then I believe all you need is the Serializable attribute.
Answer by jarenas · Mar 27, 2013 at 01:31 PM
Hi,
And... what is the answer if I need Spawnable class like Monobehaviour?. I have the same problem and, in my case, I need to detect mouse behaviour over the element. I mean, imagine I have a GUI and a MenuPage and I have a list of controls, basically, this controls are buttons and I have a MenuButtonGUI class for it. Then, like Monster class, I have a MenuPage class to containing a List. When I write a sise in the list I watch a new element in the list but I cannot setup its properties. I have a "class MenuButtonGUIEditor : Editor" too and I tried with it to show and edit the properties of the buttons inside the MenuPage class without success. I have created a "class MenuPageEditor : Editor" to edit the properties of all, the MenuPage class and the properties of elements in the MenuButtonGUI list as well but it seems it's not the way.
Any Idea?
Regards, Jordi
Please don't post questions as solutions - this is not a message board.