- Home /
How to sort a built-in array in the inspector?
I've been having this problem for quite a long time now, and I'm getting tired of trying to put together workarounds that don't even work. I've also been able to find this question posted a few other times in the past, but none of them have an absolute answer.
My problem is that when I lock the inspector and drag over a large selection of numbered items from the project pane to populate a built-in array I have, it adds all of them in a random order, very very far from the order which they're numbered in.
For example - a large quantity of textures with numerically ordered file names such as "img_001, img_002, img_003, img_004, etc." will become scrambled once dragged into an array in the inspector, leaving them a mess and in no order whatsoever.
Now my question is - can I reorganize all of them into numerical order in the inspector?
Dragging them over one-by-one or individually dragging them around inside the inspector is not an option, as I have a total of 1,071 numbered textures I need to populate an array with. Now, as I've already said, I've been able to find this question asked by a few other people. Here, here and here. However, none of them had a straightforward answer. The script provided in the second link is not an editor script, and my script that holds and uses these textures is written in JS, so the possibility of me successfully converting that C# script to JS and implementing it into my own script and it actually working is pretty much non-existent. I know almost nothing when it comes to C#, and I get all kinds of errors that I can't even understand. The script found in the third link is really only part of a script, and as much as I hate asking for something like this without providing my own rough draft to improve upon, I just can't get this working no matter how many different ways I go about it. It seems pretty ridiculous to me that there isn't already a built-in option/button for this, and I'm getting extremely frustrated over the lack of information I've been able to find on it. I've never made an editor script, let alone a custom inspector. Could someone please help me figure out a way to quickly sort things by name in the inspector? Thanks in advance.
I would need to see your numbering scheme. Usually, the default is for computers to order things in lexical order. This is not the same as you might think.
If you have three strings:
foo1, foo2 and foo12 lexical ordering would be:
foo1
foo12
foo2
If you want lexical ordering to match numerical ordering three things have to be true:
(1) The numbers must be a suffix (2) The prefixes must be identical (3) The numbers must all have the same number of digits.
As an example if ins$$anonymous$$d we had foo01, foo12 and foo03 then lexical order would be the same as numerical order:
foo01
foo02
foo12
See?
Yup, he added the names so i can see that. But it needed asking because no sorting was likely to help if the lexical ordering was wrong.
Answer by Eric5h5 · May 17, 2014 at 01:48 AM
A slightly crude, but simple way:
@script ExecuteInEditMode()
var textures : Texture2D[];
function OnEnable () {
System.Array.Sort (textures, TexNameSort);
}
function TexNameSort (a : Texture2D, b : Texture2D) : int {
return a.name.CompareTo (b.name);
}
So whenever OnEnable is called in edit mode, the textures array is sorted by name.
Perfect! This does exactly what I wanted, and it's incredibly simpler than I thought it would be. I had already tried Sort()
back when I was trying to put together something that would do what I wanted at runtime, but it was rather difficult tracking down information that would help me understand how to use it in this context. Thank you, Eric5h5! You're a 'project saver', as always.
Answer by JoeStrout · Apr 20, 2015 at 04:54 PM
FWIW, in case somebody else stumbles across this question as I did, here's the solution I have settled on for now. In the script containing an array property, add a bit of code to create a contextual menu, like this:
[ContextMenu ("Sort Frames by Name")]
void DoSortFrames() {
System.Array.Sort(frames, (a,b) => a.name.CompareTo(b.name));
Debug.Log(gameObject.name + ".frames have been sorted alphabetically.");
}
In this example, I have a "public Sprite[] frames" property; just change all occurrences of "frames" to whatever your own array property is, and it ought to work. (Also, this is C#; changes would be needed for JavaScript.)
This lets the designer decide when they want to sort, by right-clicking the script and choosing the new "Sort" command the above code adds to the contextual menu.