Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to change the UI rendering order without sibling index?

Discussion in 'Scripting' started by RusselJohns, Sep 25, 2016.

  1. RusselJohns

    RusselJohns

    Joined:
    Sep 25, 2016
    Posts:
    2
    Is there a way I can change the UI rendering order without having to mess with hierarchy orders? Like I know it's possible to change it by using changing sibling index of gameobjects but I am not sure if this can be done differently?
     
  2. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    One way to do it is by adding a canvas to the gameobject and override the sorting order of the parent canvas. Do note that this sub-canvas will also need a GraphicsRaycaster for input, and it will break batching (requires more drawcalls).
     
    st-VALVe likes this.
  3. RusselJohns

    RusselJohns

    Joined:
    Sep 25, 2016
    Posts:
    2
    I am dealing with a lot of UI components here and this will certainly will be a lot of work and draw calls.
     
  4. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Change the z axis. With ui its used for depth.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    I'd be interested to see how you are setting this up, as UI elements are always drawn based on children order to my knowledge.

    @RusselJohns I don't believe there is a current way to do so. You can use SetAsLastSibling or SetAsFirstSibling if you just need to swap stuff to the front or back, but as mentioned, if you want to change the draw order of children under a single canvas, you have to move them around based on index (if you want to tuck something in between two other children for example.

    Note that using transform.GetChild(number) will get the child in that position. So GetChild(3) will get the fourth child. GetChild(0) is the first child.

    Is there a reason you can't use the sibling index?
     
  6. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389


    Your right in that it uses childrens order when they all have the same depth but in the inspector place a couple of buttons over each other and move them in the z axis you will notice you can bring a button on top of anothet without worrying about child ordering.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    I'd like to see what you are doing differently. Because doing this doesn't change the order they are rendered. You might see them shift in 3D view, but not the actual render order if all you're changing is the z axis.

    If I create a single canvas, toss two buttons on it and then try to pull the first child to a positive z so it's in front of the second child, the second is still rendered after the first child, thus putting it in front.
     
  8. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    T
    Im away till wednesday so this is mainly from my own experience. Ill take alook when I get back and see what Ive done differently.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Sure thing. Yeah, because I have never experienced this, so I'm just curious as to how that is working for you. Always good to discover new tricks.