- Home /
Canvas override sorting issue.
Hello guys! I have a problem with sorting Images. I know that you can change order with chaning position in hierarhy, but i should set as child my panel between background and other panels to different gameobjects all the time, so i just added 2 canvases and enabled override sorting, and works like a charm, but whenever i disable/enable object, sorting doesnt work anymore, only way to change this in runtime change sorting order number manually/via scripting. How can i fix this? Or why does this happen?
You could always add a call to the sorting in your OnEnable() of the canvas. Though to be honest, you should never have more than one canvas in the same space-type, it just doesn't make sense when your entire GUI can and should exist in a single canvas.
I agree with you, i wanted to have single canvas, but how would you change order of sprites if chaning pos in hierarhy is problematic?
You could create a series of empty gameobject and use them as a dynamic layering system, childing whatever is intended to be in the front to the front object, and the rest to the various back layers. Even create a Layer script, so you can keep awareness of when objects need to be moved to a lower layer or up a layer.
As an example, lets say this is our Layer class
public class Layer : $$anonymous$$onoBehaviour
{
public GameObject childElement;
}
And this was our GUIELement class attached to each Panel like Inventory, Questlog, etc.
public GUIElement : $$anonymous$$onoBehaviour
{
void OnEnable()
{
LayerSystem.instance.$$anonymous$$oveToTop(this.gameObject);
}
}
And this would be our LayerSystem
public class LayerSystem : $$anonymous$$onoBehaviour
{
public static LayerSystem instance;
public Layer[] layers;
public Canvas mainCanvas;
void Awake()
{
instance = this;
if (mainCanvas == null)
{
mainCanvas = FindObjectOfType<Canvas>();
}
}
public void $$anonymous$$oveToTop(GameObject element)
{
if (layers[layers.Length -1].childElement != null)
{
if (!layers[layers.Length -1].childElement.activeInHierarchy)
{
layers[layers.Length -1].childElement.transform.parent = mainCanvas.transform;
}
else
{
$$anonymous$$oveDown(layers[layers.Length -1].childElement, layers.Length -2);
}
layers[layers.Length -1].childElement = element;
element.transform.SetParent(layers[layers.Length -1].transform);
}
}
public void $$anonymous$$oveDown(GameObject element, int i)
{
if (i >= 0)
{
if (layers[i].childElement != null)
{
if (!layers[i].childElement.activeInHierarchy)
{
layers[i].childElement.transform.parent = mainCanvas.transform;
}
else
{
$$anonymous$$oveDown(layers[i-1].childElement);
}
}
layers[i].childElement = element;
element.transform.SetParent(layers[i].transform);
}
else
{
element.transform.SetParent(mainCavas.transform);
}
}
}
Sorry if it seems a bit convoluted, just wanted to make sure it was ironclad.
I have the except the same issue. With my world space UI, it looks okay from the start. But whenever I disable and enable the canvas, z-sorting is not correct any more, even when I add a lot of z-space between the elements.
I am using 5.6.0f3 .
Answer by HuntNight · May 20, 2017 at 08:47 AM
Have the same problem. I have soleved it this way:
// Use this for initialization
void Start () {
GetComponent<Canvas>().sortingOrder++;
GetComponent<Canvas>().sortingOrder--;
}
// Update is called once per frame
void Update () {
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612121708im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What sets Canvas.renderOrder and why would it not respect the sorting order? 0 Answers
UI object drawing order is different between Unity Editor and Standalone version 1 Answer
4.6 UI sort order in fornt of IMGUI/OnGUI possible? 2 Answers
Transform to RectTransform Conversion 1 Answer
Using Canvas/Gui with splitscreen(several cameras) 0 Answers