- Home /
array inspector reordering
My component has an array property for an ordered list of other GameObjects that it will work with. When I need to change the order of those game objects, it's a bit of a pain. In the worst case, moving the last one to the first slot, I need to reset every single element. Is there some other way of doing it? It'd be slick if I could drag elements around in the list, but that doesn't seem to work.
i never understood why they didn't replicate the Script Execution Order editor for arrays in the inspector. DragDrop reordering thanks!
Answer by Jesse Anders · Oct 31, 2010 at 04:33 PM
You might try using a List instead. IIRC, the inspector interface for List reflects that its interface is substantially different from that of an array (for example, I believe you can delete items in the list freely without affecting the values of other items). This still won't allow you to drag items around as you describe, but it might be an improvement over what you have currently.
Another option might be to create a custom inspector or editor window that facilitated whatever operations you might need to perform. For example, you could create an editor window where you can select items in the list and then click 'up' or 'down' buttons to move them up or down in the list. (I've used this method myself, and it worked fine.)
Maybe someone else will be able to off a better suggestion though.
$$anonymous$$y suggestion is to request it. http://feedback.unity3d.com/
List seemed to do the same thing as an array, but it turned out it wasn't a big deal to write a custom editor for this one case. It came out looking like this: http://www.pasteit4me.com/1473001
I don't suppose it's possible to do make Unity use a custom editor on all arrays all over the place, is it?
Hey Jesse Anders, I know this post is quite old, but I've been trying to do this exact thing for days now and can't figure it out. Your solution is exactly what I've been trying to do. You mention you've done it with the buttons for up and down before. How did you get which item is selected? or how did you display the array contents for that matter?
I'm pretty sure I still have it... but I can't remember which project I was working on at the time, which makes it really hard to find :/
Answer by numberkruncher · Oct 03, 2013 at 11:50 PM
I have created a custom control which does allow items to be rearranged using drag and drop:
Reorderable List Editor Field for Unity:
https://bitbucket.org/rotorz/reorderable-list-editor-field-for-unity
Screenshot:
Very cool! I'd vote that up, but I have no reputation. :\
I use that one too. Best solution I've found. Need to create a custom inspector, but it's worth that small effort.
Answer by Michael-Ryan · Dec 09, 2011 at 04:32 PM
While there is no easy way to move elements around, you are able to duplicate existing array items and remove elements from the array using some shortcut keys.
From the Unify Community wiki:
Builtin arrays are the only reasonable way to allow artists and level designers to define lists of things without touching code or making your own editor extensions. However, editing long arrays can get tedious if you are relying on things being in a certain order and you need to make changes later.
There are two undocumented functions you can use when editing arrays:
Command-Delete (Ctrl-Backspace on Windows): Remove the selected element in an array. The element will be deleted, the array length will be reduced by one and subsequent elements will each have their indices reduced by one.
Command-D (Ctrl-D on Windows): Duplicate the selected element. The element will be duplicated, with the duplicate inserted after the selected element. The array length will be increased by one, and all subsequent elements will have their indices increased by one.
I have no idea why these functions are undocumented and only available via shortcut, but they can save your day when you realize that element 4 in an array of length 50 shouldn't be there.
Answer by Zogg · Dec 18, 2011 at 09:43 PM
Actually, Ctrl-D works on Windows, but Ctrl-Backspace doesn't.
Use Shift-Delete to delete on Windows (Using 3.5 beta).
On windows, you can simply press the 'Delete' key ins$$anonymous$$d of Ctrl-Backspace :)
Answer by Eric5h5 · Oct 31, 2010 at 05:16 PM
As far as moving the last one to the first slot, you can duplicate the first slot, change the value of the duplicate, and delete the last slot. You don't need a List for that; it works fine with an array.
Depends how simple the "value" is. I have arrays of big complicated structs, and they're nested (so CustomEditor can't really help). Time to write the array reordering window suggested above.
Ugh. Done, but it's so ugly, and SerializedProperty is so under-documented and full of gotchas, that I'm not going to post it anywhere.