Search Unity

Bug 2D nested - sort rendering

Discussion in '2D' started by Santako, May 15, 2023.

  1. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17
    Hi!

    In my project I have a "Sorting Group" component and, as a child, another GameObject with another "Sorting Group". I've noticed that, by default, this second nested "Sorting Group" is superseeded by the parent one.

    But i've also noticed that if I disable and re-enable the parent Sorting Group while in-game it works as expected. Unfortunately, if I try to re-enable by code this solution is not working anymore.

    Any idea why is this happening? Its a bug?

    Thank you,
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
  3. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17
    Hi, thanks for answering.

    I have simplified the case so its easier to understand.

    I have also read about nesting sorting groups documentation but this is not working (at least how I expect).

    Ok, I have 3 items in my scene:
    White circle (with sorting group at sorting layer = Background)
    Child(0): Red square (with another sorting group at sorting layer = Foreground)

    Green Hexagon (set to sorting layer = Default on SpriteRenderer)

    I expeted that the sorting group component of square overrides the parent (circle) but it doesn't as it remains below the hexagon at "default" layer.

    I've seen that latest version of unity have a feature on "Sorting Group" component that resolves this. I've tried this way but this means to update a lot of things on the projects which is not much convinient.

    upload_2023-5-17_23-8-0.png
     
  4. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    That's sorting group doing it's primary job -> grouping stuff together and preventing objects that are not part of group from being inserted between the group objects. Adding additional groups doesn't change the fact that stuff is inside the bigger group. Any sorting settings specified for child elements of sorting group (including nested sorting groups) only affect the order within group. Sorting group isn't a tool for bulk assign sorting layer.

    Don't take it personally if you already know the following stuff. I am not a mind reader and it's faster to just write everything than asking you dozen more questions to clarify how much exactly you know already.


    In a simple case like you described above the solution would be to not use the sorting groups at all and specify the sorting layer directly in the spriteRenderer component. Using sorting group on single sprite is useless. But I assume your actual uses case is a bit more complex involving multiple sprites.


    A different solution is to slightly reorder your hierachy and add an empty game object at the root of hierachy instead of white circle with sorting group. I assume the red square is parented to white circle because you want them to move together. Something like this:

    Code (CSharp):
    1. * MovingRoot -> empty game object
    2.    - Sorting group1 (Background)
    3.        ** white circle 1
    4.        ** white circle 2 ...
    5.    - Sorting group2 (Foreground)
    6.         ** redSquare1
    7.         ** redSquare2
    8. * Green hexagon (Default)
    I assume you have multiple sprites under the sorting group, otherwise the sorting groups can be replaced by specifying the sorting layer and order in layer directly.

    The same pattern is very useful in many other situations like preventing scale, rotation, mirroring or other hierachy based effect from being applied to some of the child objects.


    Third solution is not to parent stuff at all and have a very simple script which manually syncs the position of floating overaly element.
     
    Unifikation and Santako like this.
  5. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17
    Hi,

    It seems I was missunderstanding the way the nested Sorting Group works.

    It seems it has been under discussion for a while:
    https://forum.unity.com/threads/discussion-an-option-to-ignore-parent-sorting-groups.1175957/

    Finally, it seems latest version of unity includes this feature on Sorting Groups (its called "set as root" I think).

    For solving the problem I have re-organized the hierarchy as you suggested. Not the cleanest solution in my particular case but upgrading is not an option for me (I've tried but its too much new work).

    Thanks for all.
     
    Unifikation likes this.
  6. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,087
    I know this pain.

    And the sorting order issues. And there's more you'll come across soon. Especially if you use any kind of transparency or depth effects, or masking. Or want to work on occlusion culling. Lots of fun.
     
    Santako likes this.
  7. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17

    Thanks for the encoraugement hahaha
     
    Unifikation likes this.
  8. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,087
    When you get stuck on any of the above, ping me and I'll try to remember how I've dealt with each. My biggest tip: use the z space! Unity3D used to be the name of the engine... that 3D part is your best friend when working in 2D.
     
  9. Santako

    Santako

    Joined:
    Apr 13, 2018
    Posts:
    17

    Hey! Thanks for that, I will take into account.