• 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
0
Question by GreenCell · Jul 02, 2017 at 07:57 AM · editorselectionorder

Editor selection order

Is there a way to get the selection in the order that a user selected it? I'm really hoping I don't have to track selections with any callbacks. It's 2017 after all! I did see some other threads on this but they're old and didn't really have any good solutions.

As a simple example, renaming a selection of objects like this will do it in some other order. So I'll get objects renamed as newName001, newName000, newName003, newName002, even though they were selected in sequence.

 GameObject[] objs = Selection.gameObjects;
 
 for (int i = 0; i < objs.Length; i++) {
     objs[i].name = string.Concat("newName", i.ToString("000"));
 }

Comment
Add comment
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

2 Replies

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

Answer by Bunny83 · Jul 02, 2017 at 09:46 AM

No, as far as i know the editor does not track any order of your selection. The selection can be done in various ways where the order is not necessarily given. Also adding and removing items from a selection would make it difficult even for the user to determine the correct behaviour. Also "Selection.gameObjects" is already a filtered list as it only returns gameobjects. However the selection could include other items such as folders or other assets (I', talking mainly about the project view).

If you want to implement a mass rename feature it would be the best to use an editor window or a wizard which the user opens and then select the objects in the order you want. This also allows a visual feedback and the reordering of the items in the wizard / editor window.

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 GreenCell · Jul 03, 2017 at 03:42 PM 0
Share

Oh no, not the answer I was hoping for! Oh well, thank you for the advice, much appreciated. Other softwares I use like $$anonymous$$aya or 3dsmax always return the selection with the order they were picked, so I was taken aback that it wasn't the case here. $$anonymous$$akes it more challenging to make tools!

avatar image
3

Answer by Abaobao · Dec 14, 2017 at 09:28 AM

As far as I know, you should do a sort by transform.GetSiblingIndex()

Like:

 public class UnityTransformSort: System.Collections.Generic.IComparer<GameObject>
 {
    public int Compare(GameObject lhs, GameObject rhs)
     {
         if (lhs == rhs) return 0;
         if (lhs == null) return -1;
         if (rhs == null) return 1;
         return (lhs.transform.GetSiblingIndex() > rhs.transform.GetSiblingIndex()) ? 1 : -1;
     }
 }


 GameObject[] objs = Selection.gameObjects;
 System.Array.Sort(objs. new UnityTransformSort());


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

84 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 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 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 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

How to get the top most GameObject selected in Editor 0 Answers

Working with the editor Selection 0 Answers

How to render projector in front of walls but behind player? 1 Answer

Editor Selection and getting a different currentActiveGameObject. 0 Answers

Custom editor: undo change to editor property 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges