x


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.

more ▼

asked Jan 11 '12 at 09:27 PM

Fire-Dragon-DoL gravatar image

Fire-Dragon-DoL
33 3 3 5

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Jan 11 '12 at 09:55 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Correct! Data class shouldn't inherit from MonoBehaviour. 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.

Jan 11 '12 at 10:04 PM by0log1c

Your example is great and works really way, thanks a lot! (also to you Byologic). I used the MonoBehaviour 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 :)

Jan 11 '12 at 10:37 PM Fire-Dragon-DoL
(comments are locked)
10|3000 characters needed characters left

If you are not using custom inspector script then I believe all you need is the Serializable attribute.

more ▼

answered Jan 11 '12 at 09:59 PM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Mar 27 at 01:31 PM

jarenas gravatar image

jarenas
1 1

Please don't post questions as solutions - this is not a message board.

Mar 27 at 01:34 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1673
x467
x56
x33

asked: Jan 11 '12 at 09:27 PM

Seen: 1653 times

Last Updated: Mar 27 at 01:34 PM