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

Canvas overrideSorting not working on nested canvas?

Discussion in 'UGUI & TextMesh Pro' started by drHogan, Nov 17, 2016.

  1. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    200
    Hi guys,

    I am having a bit of a weird problem. I am writing a tutorial for our game Empires in Ruins, and to do so for each framestep of the tutorial, i bring forward the elements that can be interacted with, while hiding everything else behind a darker panel that prevents interaction.
    To do so, at each new tutorial frame/step, I attach a canvas to the tutorial I want to bring to the front if it doesn't have one already, and i change the sorting layer.
    For some reason, I am having an issue with nested canvases, i.e. when I set the canvas.overrideSorting to true, it stays false. Even printing the value right after the assignment it still prints false.
    Any clue on what i might be doing wrong?

    Code (CSharp):
    1. if (elem.gameObject.GetComponent<Canvas> () == null) {
    2.                             Canvas newCanvas = elem.gameObject.AddComponent<Canvas> ();
    3.                             newCanvas.overrideSorting = true;
    4.                             newCanvas.sortingLayerName = "TutorialSprites";
    5.                             elem.alreadyHadCanvas = false;
    6.                             Debug.Log (elem.gameObject.name +" , "+ newCanvas.overrideSorting);
    7.                         }
    Cheers,
    H
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Little confused, but why do you need to do this with nested canvas?
    I'm not sure if adding a canvas group with ignore parent would help or not, since this may only be for certain options.

    I would think you could simply have a tutorial canvas with a dark image as it's main child. Have this be on a higher layer than your normal canvas layer and then move stuff to this tutorial layer when you need it to be in front and then move it back when you're done with it.

    Also note that the UI components will render in the order they are listed, so the last child is always in front (in case you need to bring forth multiple objects).
     
  3. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    200
    Hi Brathnann,

    here you see for example that I am bringing forward just the subpanel of that panel to highlight it. In the same way, clicking on one of those buttons, opens up another separate panel that explains bonuses and maluses.

    My UIs are spread across different canvases with different spaces (world and camera mainly), and have a quite big hierarchical structure (being a strategy game you can imagine that it is quite a wide UI system). The simple fact of bringing something to front in the traditional way, causes me a lot of issues because of this subnesting. Moreover I am making it so that also sprites belonging to the world are brought forward in front of the dark panel, and for those the added canvas seems to be the only workaround i found.



    Here you see the case in which i opens up a panel, not belonging to the hierarchy of the Province panel opened in the background, but using the canvases, i can still place it automatically in the right "layer"



    The weird thing is that when it doesn't work from code (in the snipped, i set the ovveride to true, then print the override value and it returns false), if i set it from the inspector with the game running, it then works perfectly

    Cheers,
    Emiliano
     
  4. Hanyi

    Hanyi

    Joined:
    Apr 13, 2015
    Posts:
    1
    i get the same issue,maybe is is a bug,if you get answers,please let me know,thanks
    ps: i get the issue on unity5.6 but it works on unity5.5
     
    Last edited: Apr 10, 2017
  5. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    200
    Me I actually solved by using DestroyImmediate on the canvas instead of Destroy, it is probably related to when some behaviors happen during the frame, and DestroyImmediate seems to prevent the issue at least in my case.
     
  6. kisence

    kisence

    Joined:
    Jul 7, 2017
    Posts:
    1
    The problem may be related to the Active state,
    First detect the active state of the target object, if false, then set it to True, and then set the override sorting level, set the settings and then set it back.
    Code (CSharp):
    1.         bool oldActive = go.activeSelf;
    2.         if(!oldActive)
    3.         {
    4.             go.SetActive(true);
    5.         }
    6.  
    7.         canvas.overrideSorting = true;
    8.         canvas.sortingOrder = order;
    9.         canvas.sortingLayerName = "Guide";
    10.  
    11.         if(!oldActive)
    12.         {
    13.             go.SetActive(false);
    14.         }
     
    adamspector-jrg likes this.
  7. adamspector-jrg

    adamspector-jrg

    Joined:
    Mar 14, 2017
    Posts:
    4
    This was the issue for me as well. It seems that the overrideSorting does not work when the game object is inactive.
     
  8. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    174
    Seriously, this is still not resolve, it's stupid that to get a prefab to show in proper order you need to go that way :(
     
  9. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    124
    Still not fixed in 2022. But does anyone has filled a bug report on this ?
     
  10. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    174
    Personally, no...
    Tired of:
    1. filling bug
    2. building a specific project for them
    3. waiting month
    4. finally get an answer saying that they will look into it
    5. Later, getting an answer that they don't really understand
    6. Sometime getting an approval but need vote
    7. Wait a few year for the bug to be fix

    When I have a workaround, I don't bother with Unity bug logging. the process, when it work, make no sense. Mostly if you have a multiplayer game with a server and database component
     
    Whatever560 likes this.