Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by SirBedlam · May 17, 2014 at 01:07 AM · arrayinspectorordersort

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · May 17, 2014 at 01:26 AM 0
Share

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?

avatar image Eric5h5 · May 17, 2014 at 01:49 AM 0
Share

That's really not the issue here.

avatar image Jeff-Kesselman · May 17, 2014 at 01:56 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SirBedlam · May 17, 2014 at 02:20 AM 0
Share

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.

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

List.Sort with IComparer 2 Answers

How can I order an array of RaycastHits in reverse order of distance? 1 Answer

Dynamic Turn Order and Display Based On Initiative Value (Javascript)? 2 Answers

What is the quickest way of getting the most common item in a list or array? 1 Answer

Renaming Inspector 2D Array Element 0 , Element 1 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges