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
8
Question by Amazing_Bluie · Oct 11, 2014 at 03:58 AM · uidisplaydepthorder

New UI: How to change depth/order from script?

I need to have elements in the canvas change their display order (depth) from a script (C#). Since that order is now determined by the placement in the Hierarchy, how do I do that?

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

4 Replies

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

Answer by hu90 · Oct 11, 2014 at 01:24 PM

You have to change their order in the hierarchy indeed.

For example, this function will move the attached transform within its parent transform by delta positions (positive is forward, negative is backwards) :

 public void MoveInHierarchy(int delta) {
   int index = transform.GetSiblingIndex();
   transform.SetSiblingIndex (index + delta);
 }


A typical real world example is, say you have a number of buttons. When any one is clicked, you're going to bounce animate it, so it becomes much bigger for a half second. The one being bounced must be ahead of all the rest in z-order, or the overlap won't look good.

Fortunately it's this easy

 private IEnumerator _bounce(int whichButton)
    {
    // be sure to bring it to the "front" of its siblings
         
    RectTransform moveToFront =
       buttons[whichButton].GetComponent<RectTransform>();
    moveToFront.SetSiblingIndex( buttons.Length - 1 );
 
    ... bounce the button 'whichButton'
    }

This is the very reason Unity added these "sibling order related" calls.

Note that often, you can just use SetAsLastSibling , even easier!

Comment
Add comment · Show 5 · 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 Amazing_Bluie · Oct 12, 2014 at 02:28 AM 0
Share

Thank you very much!

avatar image Taomujw · Nov 13, 2015 at 07:32 AM 0
Share

How can I only just rearrange the first parent's and it's childs' position. Right now its moving the whole entire canvas for me. Please let me know. Thank you.

avatar image ddubois · Feb 24, 2016 at 07:19 PM 1
Share

What does SetSiblingIndex(4) do with the object already at 4? Does it shift everything from 4 onwards down one? Does it boot the old #4 to the bottom? Swap places with the transform being set?

Without knowing the implementation, I'm concerned SetSiblingIndex() might be an expensive operation - inserting into an array takes O(n) - and that repeatedly creating objects and inserting them early into the list might degenerate to O(n^2). I'm also worried that setting an index might have undesirable effects on the existing ordering.

avatar image tswalk ddubois · Jun 15, 2017 at 08:10 AM 1
Share

one drawback to this is if the objects are in a layout grid, which uses this index to arrange their order (based on hierarchy)... so if you SetAsLastSibling, yep... gets put to the last slot in your grid.

avatar image Fattie · Jun 01, 2016 at 12:14 AM 0
Share

DDubois - there are no performance concerns whatsoever. It's a game engine - it is constantly rendering massive amounts of mesh, lighting, etc. You use the call in question all the time. It's only something you use when (say) a button is pressed or some action happens - it might be used a few times per hour of play. It's a non-issue.

avatar image
5

Answer by _dns_ · Oct 11, 2014 at 01:12 PM

Hi,

You can use all the "sibling" named functions of the Transform component: http://docs.unity3d.com/ScriptReference/Transform.html

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 Amazing_Bluie · Oct 12, 2014 at 02:28 AM 0
Share

Thank you!

avatar image
4

Answer by Stardog · Aug 25, 2016 at 12:05 AM

You can now add a Canvas and change the Sort Order parameter.

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 nogebatorr · Oct 08, 2016 at 07:43 PM 0
Share

It's exactly I'm looking for. Thanks!

avatar image
1

Answer by RobertoBubaloZutec · Nov 07, 2018 at 12:13 PM

I have had a similar issue, SetAsLastSibling() worked for me!

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

11 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

Related Questions

UI Button order 1 Answer

How to click on button that is behind transparent image in New UI? 2 Answers

How to Display UI at a Specific "Depth" (Render Depth) in 3D Space 1 Answer

Camera layering : UI shader is cleared by second camera with Depth Only flag 1 Answer

UGUI custom material for some parts of the text 0 Answers


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