i got an interface here that depends on z order
i was wondering if and how something like this would be possible with the new uGUI since z order is ignored?
thanks
i guess each window needs to be a world space canvas?
If you put this script on the canvas object, it will bubble-sort the elements in the canvas, changing the order of the canvas children. (I’m comparing position.z’s here, but you can use any criteria)
int k = transform.childCount;
Transform childI;
Transform childJ;
for( int i = 0; i < k; i++ ){
for( int j = 0; j < k; j++ ){
if( i == j )
continue;
childI = transform.GetChild( i );
childJ = transform.GetChild( j );
if( childI.position.z > childJ.position.z ){
int tempIndex = childJ.GetSiblingIndex();
childJ.SetSiblingIndex( childI.GetSiblingIndex() );
childI.SetSiblingIndex( tempIndex );
}
}
}
2 Likes
thanks. i recently got it to work like this.
using UnityEngine;
using System.Linq;
public static class GameObjectExtensions
{
public static void SortChildren(this GameObject gameObject)
{
var children = gameObject.GetComponentsInChildren<Transform>(true);
var sorted = from child in children
orderby child.gameObject.activeInHierarchy descending, child.localPosition.z descending
where child != gameObject.transform
select child;
for (int i = 0; i < sorted.Count(); i++)
{
sorted.ElementAt(i).SetSiblingIndex(i);
}
}
}
i am using a single Screen Space Camera Canvas instead of a bunch of World Canvases as i expected…
the sorting would be easier with world space but then stretching to screen and reference resolution do not work i guess