- Home /
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:
So, how can I make my List<> editable in the inspector and add or delete items like a Button in the UI can?
Many thanks in advance!
I googled a lot and found nothing yet. This is the reason why I asked the question here.
I thought that this would be a native behauvior of the inspector to allow editing lists and that I might be missing something obvious.
If I have to create my own editor-script please just tell me and I'll dig deeper into this.
Edit:
I found several posts that indicate that generic lists should work with the inspector by default like here: http://forum.unity3d.com/threads/inspector-allow-us-to-edit-generic-lists-and-dictionaries.174790/
But why does this not work in my case?
I copied your DamagerLevel class, then did public List<DamagerLevel> blah
and it shows in the inspector just like an array.
Answer by RicoLoco · Jul 24, 2016 at 06:43 AM
use [System.Serializable] to serialize your DamagerLevel class and then it will show up. oops, I see now that you did.
Answer by IronarmGames_LLC · Jul 24, 2016 at 05:46 AM
You need to create a custom inspector:
Heres some information on custom inspectors:
) http://docs.unity3d.com/ScriptReference/EditorGUILayout.html
) http://docs.unity3d.com/ScriptReference/GUILayout.Button.html
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.
Answer by kunkelbek · Aug 19, 2017 at 03:59 PM
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!
Neither of the links worked for me unfortunately. However, the use of a reorderable list was shown at a Unite talk in 2016. Here's a link to the talk when the list is shown: https://youtu.be/9bHzTDIJX_Q?t=2122 The timestamp is set to where he shows what the list looks like in the inspector, but he shows the inspector code he used about 2 $$anonymous$$utes later.
Answer by ashleyjlive · Jul 24, 2016 at 03:22 AM
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.
Late but it is not exclusive. The button GUI can be implemented, I did it in a few $$anonymous$$utes for my custom cutscene sequencer. It just requires knowing a few things that can be easily learned in a day. Hope this helps anyone who stumbles across it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Inspector scripting, adding lists, checkboxes and buttons [With Image] 0 Answers
Difference between assigning a value in inspector and with a custom editor script 1 Answer
Adding an item to a Generic List copies the old one 1 Answer
*Really* need help on Serialisation, stuck on this for over a week now, deadline looming 1 Answer