- Home /
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?
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!
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.
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.
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.
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.
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
Answer by Stardog · Aug 25, 2016 at 12:05 AM
You can now add a Canvas and change the Sort Order parameter.
Answer by RobertoBubaloZutec · Nov 07, 2018 at 12:13 PM
I have had a similar issue, SetAsLastSibling() worked for me!
Your answer
![](https://koobas.hobune.stream/wayback/20220613164829im_/https://answers.unity.com/themes/thub/images/avi.jpg)