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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI sorting with instantiated UI elements (how to make a instantiated canvas on top of everything?)

Discussion in 'UGUI & TextMesh Pro' started by Thaao, Oct 10, 2015.

  1. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    I have a menu canvas that when activated instantiates a lot of buttons from a button prefab at runtime based on the player's inventory (each item gets its own button). When you 'click' one of these buttons, in instantiates a prefab of a canvas with some more buttons on it, a sort of context menu for that button. I set the context menu to be a child of the item button that it came from.

    The problem is that all the other item buttons are displaying on top of the context menu. I've tried setting the sorting order in the context menu's prefab to a higher number, changing the sorting layer to a new layer at the bottom of the layer list so it should display on top... but it doesn't seem to work and the buttons still appear on top of the context menu.

    What is a solution for this? Am I missing something? I realize it's not at the bottom of the heirarchy because it's a child of the button that instantiated it (and with the way it displays, all the buttons it overlaps will be lower in the heirarchy). But shouldn't sorting orders fix this...?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    Is there a particular reason why you're adding the "context menu" under the button object that created it ?

    AFAIK, the canvas renders the UI elements in the same order they're listed in the hierarchy. In this case, the context menu can be placed last, thus appearing on top of everything else.
     
  3. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    I want the context menu's position to be/stay relative to the button.

    I realize that they do draw in the order of the hierarchy, but sorting layers and sorting order are supposed to override that it seems. I've been able to do it with other UI elements when I set them in the inspector, like making a child appear behind its parent.

    In this case, the prefab for the button is on the default sorting layer and sorting order is 0, and the prefab for the context menu is on the "Top" sorting layer (that's the name -- it's the last in the list) and the sorting order is 1. No matter how I seem to set these up, though, the hierarchy is the only thing that seems to determine the draw order.