Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

UI elements sort order

Discussion in 'Getting Started' started by viveleroi, Jan 11, 2017.

  1. viveleroi

    viveleroi

    Joined:
    Jan 2, 2017
    Posts:
    87
    I'm creating some Image elements from C#, they're dynamic backgrounds for a text element. The image objects are being added to the same parent GameObject the text lives under.

    No matter what I do, I can't get the text to render on top of the Images. Since the text doesn't have a sortOrder I've tried setting the z positions on all elements, but it doesn't make a difference.

    There are no errors or warnings in the console.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,481
    Render order for UI elements is based on the hierarchy order. So move items up or down the hierarchy to change when things get rendered.

    Its different from the rest of Unity, just to be fun.
     
  3. viveleroi

    viveleroi

    Joined:
    Jan 2, 2017
    Posts:
    87
    I must be doing something wrong then. When I made this post, I was adding the Image elements to the parent of the Text element. Just in case unity doesn't like the fact that the Image and Text became siblings, I made a new GameObject to nest the text under. That doesn't work either.

    -> TextBubbleBackground <- Image elements added via code here.
    -> GameObject
    -> TextBubbleText

    No matter what the Image(s) always cover my Text.

    EDIT: Playing around with transform SetAsFirstSibling / SetAsLastSibling methods. Even thought the objects are no longer siblings, they work as long as I set the index for every sibling.
     
    Last edited: Jan 11, 2017
  4. CuirassEntertainment

    CuirassEntertainment

    Joined:
    Aug 29, 2017
    Posts:
    1
    If I am using a grid order layout and want my objects in a specific order, simply changing their order in the hierarchy will not work, they will be physically moved. Is there another option?
     
    DrViJ and Menion-Leah like this.
  5. VLukianenko

    VLukianenko

    Joined:
    Mar 27, 2017
    Posts:
    27
    I think Canvas component with Override Sorting option checked would help with this.
     
  6. mikewilliams76901

    mikewilliams76901

    Joined:
    Jan 29, 2014
    Posts:
    3
    VLukianenko's suggestion worked for me. Many thanks!
     
    VLukianenko likes this.
  7. LanVision

    LanVision

    Joined:
    Dec 3, 2018
    Posts:
    14
    Please mind the downsides and consequences. Every another canvas breaks batching thus increasing draw calls amount. One call for things below the new canvas, one for the new canvas with children and one for something on top of it. This may really kill performance if it is done say in a backpack item prefab. Even a small backpack with 8x8 items may result in 3*64=192 draw calls made (instead of just one!) just to render your backback.
     
  8. VLukianenko

    VLukianenko

    Joined:
    Mar 27, 2017
    Posts:
    27
    Unity Team at Unite recommended breaking things into smaller canvases for better performance, since every little change in one big canvas makes the whole canvas redraw.

    References:
    - Unite
    https://doozyentertainment.zendesk.com/hc/en-us/articles/360002576153-UI-batching-in-Unity - some blogpost, which says same but in text.
    "So if you use one big canvas for your entire UI then you are doing it wrong."

    While you don't need to make every little item a separate canvas, since that'll introduce problem that LanVision described, but you better divide your big canvas into separate smaller canvases that have own update logic, independent from other elements.

    That is, however, a 2 year old info. Please let me know if this have changed.
     
    LanVision likes this.
  9. LanVision

    LanVision

    Joined:
    Dec 3, 2018
    Posts:
    14
    No, that hasn't changed. However, following recommendations blindly isn't going to do any good if one doesn't understand the underlying reasons.

    Dividing canvases can greatly increase performance if there are changing layout elements. If a layout element has changed, these changes will "propagate" to parenting layouts in the same canvas. This usually means that the whole canvas is going to be recalculated. So it is generally a good idea to enclose such changing elements into a separate smaller canvases because such layout changings won't go beyond the canvas enclosing it.

    So, canvas dividing is good when there are too many UI calculations which can be constrained into smaller canvases.

    But think a minute about a general backpack full of items. They are usually just put in their places once and aren't really going to change after that. There are no layout changes and no propagations going to happen at all in this case. So there really is no need to produce any more canvases besides just one. But the added draw calls overhead may and will decrease performance here (more calls means more impact).

    All in all, fellow developers, please think of what you are doing, even if it is 'recommended' by Unity team. Because every such recommendation is intended to solve some problem which probably doesn't actually exist in your project. And bear in mind the price you may have to pay for implementing them.

    EDIT: By "changing" I of course mean changes in size, positions etc, not changing one image to another one.