Search Unity

Bug UI sort order different across scene and game view

Discussion in 'UGUI & TextMesh Pro' started by TheVirtualMunk, Jun 15, 2022.

  1. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    As title says - take a look at the screenshot below in playmode; The black area is correctly rendered in the scene view but wrong in game view.
    upload_2022-6-15_12-42-42.png
    Changing the canvas sort order in the editor during runtime will solve this issue, but that's not really a workaround for builds.
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Unity has at least half a dozen different sorting rules based on what kind of objects you are working with and how they are organized. If you want any meaningful advice at least show and tell how you have those UI elements setup. Otherwise all you can get is random guesses or copy pasted blocks of documentation.

    In simplest case UI elements within each canvas are sorted by their order in transform hierarchy. Do you have any scripts which create and destroy parts of that UI or otherwise change the tranform hierachy during play mode?

    You mentioned canvas sort order, is the UI you showing split across multiple canvases? Do multiple of them have same sorting order? What prevents you from assigning required canvas sort order in edit mode?
     
  3. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    Sorry, I see the confusion without extra details. The reason for omitting details is I feel like I have a good grasp on UI in general (4 years of experience) and I've never seen this behavior so my conclusion is it must be a bug or some other weird edge case behaviour. Especially because the screenshot above is taken during playmode (it's one screenshot, not two), meaning that scene view and game view are reporting two different realities, which in itself is a bug to me no matter the reason behind it.

    The thing about flipping the rendering order in the editor during playmode is this;
    1. From starting playmode the canvases are rendered wrong in the game view but correct in scene view (the screenshot above)
    2. I check the different canvases (the transparent container with the text is on its own sub canvas with overwrite sort order)
    3. I see that the sort order is set to -1 while the parent sub-canvas (the icon with the car) is set to 2
    4. I toggle the -1 canvas to 1, and everything renders correctly
    5. I now toggle it back to -1 and everything STILL renders correctly
    6. My conclusion is something is wrong with the initialization of canvases and their render order
    And yes, I guess I could make a component for toggling back and forth the sort order, but that's not solving the root issue.
     
  4. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    So I just tested a script that goes like this;

    Code (CSharp):
    1.  
    2. [ButtonMethod]
    3.     async void Toggle()
    4.     {
    5.         await Task.Delay(1000);
    6.         canvas.overrideSorting = false;
    7.         await Task.Delay(1000);
    8.         canvas.overrideSorting = true;
    9.     }
    to toggle the canvas during play mode - but this DOES NOT WORK. E.g. I can't make components to hacky fix this issue - I HAVE to toggle "override sorting" manually to fix this bug, which of course is unacceptable for builds.

    Here's a gif of the issue;
    ui sorting issue.gif


    Also,
    LayoutRebuilder.ForceRebuildLayoutImmediate();
    doesn't help
     
    Last edited: Jun 16, 2022
  5. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    That indeed doesn't seem right. Especially if increasing sort order from -1 to 1 makes it move to to the back since lower numbers are supposed to mean more towards back. Now I know what you meant by changing canvas sort order an I also I wouldn't want that since -1 and 1 are both supposed to be behind 2.


    Just to be clear in the video above you toggled sort order for parent subcanvas, the one with icon and sorting order 2 not the text box which is supposed to have -1 ?
     
    TheVirtualMunk likes this.
  6. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    Correct - I noticed that it doesn't matter which canvas I change (as long as it's one in the hierarchy with the problem) and it also doesn't matter which option I change (sort order, override sorting, or even additional shader channels) - there must be some kind of UI refresh that is only running when interacting with the inspector, which doesn't trigger from script.
     
  7. TheVirtualMunk

    TheVirtualMunk

    Joined:
    Sep 6, 2019
    Posts:
    150
    Bug report CASE IN-7279

    This is unity 2021.3.4f1 in case anyone is wondering
     
    Twyker likes this.
  8. unity_3720927166C60D40D37C

    unity_3720927166C60D40D37C

    Joined:
    Feb 25, 2022
    Posts:
    1
    I also encountered the same issue (unity 2021.3.9f1). I could not find the bug report on the issue tracker. Have there been any news?
     
    brunoliveiraaraujo and Twyker like this.