Editing a List<> in Inspector

Hi.

I have a class with a List<> and I’d like to be able to edit this list in the inspector. I’d like to be able to add, alter and delete objects in this list.

Unfortunately the inspector only show the size of the list but not the objects itself.
If I use an array instead of List<> I can edit the items even thow I could not add or delete items.

These are my classes:

public class DamageManager : MonoBehaviour
{
	public List<DamagerLevel> lstDamagerLevels;// = new List<DamagerLevel>();
	public DamagerLevel[]  TestWithArray = new DamagerLevel[5];

}

[System.Serializable] 
public class DamagerLevel
{
	public float neededDamageInPercentage = 50f;
	public Sprite damageSprite;
}

This is what it looks like in the inspector:

51357-1.png

So, how can I make my List<> editable in the inspector and add or delete items like a Button in the UI can?

51358-2.png

Many thanks in advance!

use [System.Serializable] to serialize your DamagerLevel class and then it will show up.
oops, I see now that you did.

You need to create a custom inspector:

Heres some information on custom inspectors:

But basically in your custom inspector you are going to need to have 2 functions, one for adding a new entry to the list at an index and one for removing entries from the list at an index.

Now what you can do is wire up some GUI.Button as a “+” and “-” symbol to simulate a add and remove button, then basically use a foreach to create a layout containing these 2 buttons for each element in your list. This way, you can have a “+” and “-” button for each element in your list.

You should really look into the custom inspectors and how they function using OnGUI and Layouts.

It seems I’m a little late to the party, so I’m assuming this thread is no longer valid.
For anyone who is (like me) also searching for a solution to this problem:

What you’re looking for is Reorderable Lists.

Sadly Unity has (as far as i know) no documentation on these lists, but I have found another site that explains them very well.

You can find this page here.

hope this helps!

I think you are mistaken.
The button GUI is specific for UnityEvents (similar to a delegate) and has nothing to do with a list/array.

Lists are treated exactly the same as Arrays in the GUI (not code).
The Add/Remove feature of a list does not exist in the Unity GUI. Instead you can define a size of the list and then fill in the data for each element in that list. Additionally, if you wish to delete a specific element in the list you can right click on it and select remove.