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

Official Tell us how you're using the Sorting Group Component

Discussion in '2D' started by rustum, Mar 5, 2019.

  1. rustum

    rustum

    Unity Technologies

    Joined:
    Feb 14, 2015
    Posts:
    190
    Hiya everyone!

    The 2D Team at Unity is researching future developments for the 2D feature-set.

    To begin with we are looking at the foundational pieces. One of these is sorting. Specifically we’d like to get a better understanding of how everyone is using Sorting Groups.

    Sorting Group Component.png

    Here are some questions to get this discussion started:
    1. Are you using the Sorting Group component?
    2. If so, how are you using it? What problem are you solving using Sorting Groups?
    3. Are you using nested Sorting Groups?
    4. If so, how are you using them? What problem are you solving using nested Sorting Groups?
    5. If we removed the ability to nest Sorting Groups, how would this impact your productions?
    If you would rather answer these questions in private, please follow this link to answer a short survey.
    https://goo.gl/forms/JapvJL8J6oD2cMat1

    We're looking forward to hearing from all of you!
     
  2. Neo-Dragon

    Neo-Dragon

    Joined:
    Feb 29, 2016
    Posts:
    17
    Hey Rus.

    I've been using "nested" sorting groups .
    I have characters made up of several pieces (arms, body, head etc.) in a sorting group called "character meshes" and then have that character in a parent object which uses the sorting group component to set the character into a "World" layer in a 2D map . From there I have a simple script that sets the Order value based on the Y position.
    This way all characters are placed in the same sorting group layer and are correctly order sorted for moving on the map.

    I've no real issues with this as it is but I'd love if you could set an enum if you wished to pass in the transform to the order value .
     
    rustum likes this.
  3. Iain-Lobb

    Iain-Lobb

    Joined:
    Jan 11, 2013
    Posts:
    30
    This has actually reminded me about this feature! I will definitely be using it on current project to fix a couple of sorting issues (characters holding weapons). I won't be using nesting but I can see the value there.
     
    rustum likes this.
  4. orchard800

    orchard800

    Joined:
    Jul 28, 2015
    Posts:
    19
    I've only started using this. I have a 2D top down (like zelda) where my character can walk behind objects like trees and walls. I'm using multiple sprites for body parts and weapons so this is great to stop my character's body parts fighting with the trees :)
     
    kojoopuniLA, SiriAnimates and rustum like this.
  5. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    105
    Doing top down games I put sorting group components on character-parent(while the child objects just use sorting with sprite property) and objects like trees, bushes etc. so the character can sort with those when walking in front and behind.
    No problems so far and don't really have any suggestions on how to expand the function.
     
    SiriAnimates and rustum like this.
  6. eightpoundgorilla

    eightpoundgorilla

    Joined:
    Jan 24, 2014
    Posts:
    5
    I actually stumbled across this component accidentally. I'm using the Anima2D skeletal animation framework which, similar to some of the other posters, composites multiple sprites to make up a complete character. Each of these sprites uses their own "order in layer" value which is fine until I pull Unity's 2D tilemap system into the mix. I'm using isometric tilemaps and custom axis (y-axis) sorting which works great unless you have a skeletal character with different body parts with different order values. I just started searching through components for something that sounded like it might solve my problem and found the Sorting Group. Brilliant!

    On a side note, I was also playing with the Unity 2D Animation Package preview which actually feels pretty good (and it's native), but I'll probably wait until it goes full release to commit to it. I've also only been playing with game dev for a few months so maybe take my endorsement with a grain of salt.
     
    rustum likes this.
  7. Th0re

    Th0re

    Joined:
    Sep 4, 2015
    Posts:
    39
    This is amazing. Found out about this by accident, too, immense time and sanity saver for me. Works perfectly for my purposes. I'm using it to group characters and props together, so they are drawn as one complete entity. This is important, because it would otherwise be almost unmanageable once you have multiple characters each made of out different sprites, and you cannot just shove them to different layers.
     
    rustum likes this.
  8. tiddlesworthgames

    tiddlesworthgames

    Joined:
    Dec 8, 2017
    Posts:
    3
    This has saved my project basically. I have a Grid Generator custom tool that would use a hex sprite prfab to make a flat top hex grid in the editor. I set the renderers sorting order and in editor it looked exactly how it was supposed to, but as soon as i hit play all of the cells would reorder themselves and ruin the map i had just made(many of the sprites are larger than the Hex). I finally saw this thread and just added a sorting group when i instantiated my grid and it fixed it. the cells now keep their rendering order when i play the scene. My project would have ended there without Sorting Groups.
     
  9. Th0re

    Th0re

    Joined:
    Sep 4, 2015
    Posts:
    39
    I now have what I think is a bug with Sorting Group. For one, my setup behaves 'differently' now for no apparent reason, than it did a few days ago. I am not aware I made changes, but I might be mistaken. Restarting has no effect.

    Bug Description
    Sorting Group has no effect (anymore) when a game object with sprite renderers is put as child under it during runtime, but takes effect when (in runtime) Sorting Group is briefly disabled and enabled again.

    I have a prefab with a Sorting Group, let's call it Item. I have a character who has a Holder game object also with a Sorting Group. In runtime, the Item is created from the prefab, and parented into the Holder. What should happen is that the Holder's Sorting Group determines where the Item is being rendered (namely to look as if carried by the character). This worked, but now the Item is rendered above everything else, somehow ignoring its parent's Sorting Group(s). I can disable and re-enable the component in runtime, and then it renders correctly. I see no difference when I remove the Sorting Group component on the item itself (I would need it when the item is dropped into the level, i.e. not carried around).

    I might be doing something wrong. Let me know, to avoid (and fix) this issue, thanks :)

    Edit: the issue disappeared, but I have no idea why (I did some refactoring on input and handling of the flipping, which also touches on parenting objects around).
     
    Last edited: May 15, 2019
  10. ColbySg89

    ColbySg89

    Joined:
    Mar 26, 2015
    Posts:
    15
    I have been using the sorting group feature mainly for making sure that certain sprites appear on top of other sprites, for example a top down game where when the player walks behind the top half of a building the player sprite would be on a lower sorting layer than the top of the buildings to make sure the player is rendered behind the building.
    I have also used this to make sure weapons are always rendered on top of enemies so that when attacking the weapon layer sprites don't compete with the enemy layer sprites.
     
  11. Th0re

    Th0re

    Joined:
    Sep 4, 2015
    Posts:
    39
    Check out this video and especially from 3mins onwards, sorting based on an axis which might be suitable for your game.
     
    frikic likes this.
  12. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,184
    1. Yes, actively in our most recent project.
    2. The game is a 2D side-scroller and we put the sorting group on character root and other prefabs such as a building root that has multiple children. The big problem in Unity is adjusting the sorting order and layer for hundreds or even thousands of objects in a scene. When one order changes hundreds of other objects may need to be changed. Sorting Groups helps keep these changes more localized, because now we only have to sort characters and buildings against each other while keeping the attached children, e.g. windows and decorations within the sorting group.
    3. We used it at some places initially but then found it a little bit too confusing and tried to limit the use of Sorting Groups to prefab roots close to the top-level or our hierarchy.
    4. Initially, we thought we could simply build some kind of hierarchy with sorting groups which would help us sort large scenes, but we soon realized that it got confusing as to which renderers were sorted against each other and which were part of a sorting group, so we stepped back and only use groups for the top-level prefab roots now.
    5. Not so much. We would actually slightly benefit from it because our custom tooling would become simpler. If overall performance would also benefit, we would welcome removing nested groups in favor of having a more easily understood sorting hierarchy with only one group per level.

    Overall, we really like the Sorting Group component, but we could do without the ability to nest them.
     
  13. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    1. Yes, a lot.
    2. In current project we have several parallax layers of environment each of which has a Sorting Group. Sorting Groups allow us to manage draw orders of complex objects with ease.
    3. Yes.
    4. The objects in each parallax layers can be complex and consist of many Sprite Renderers. Sometimes we need to change the draw order of these objects inside the layer. Without nested Sorting Groups it would be a pain.
    5. Please don't.
     
    SlimeProphet and orchard800 like this.
  14. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,850
    1. trying to sort sprites in top down view, by setting sordingOrder based on Y (and sometimes also X) coordinates.
    (image: eyes and mouth are separate child sprites. User can pickup bomb and need to be drawn on top in that case, by switching sortinglayer. Gets too complicated fast..)
    upload_2019-7-3_10-13-13.png
    2. never heard
    3. -
    4. -
     
  15. Sicinsions

    Sicinsions

    Joined:
    Dec 18, 2019
    Posts:
    11
    That's actually stunning! It really saves me a great deal of time and nerves. And it's just it for the needs I have :)
     
  16. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    My main issue with sorting groups is it doesn't 'work' when combining Sprite Renderer and Image components. Both of these should 'use' sorting correctly
     
  17. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,381
    No, what's that?
     
  18. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,394
    APSchmidt likes this.
  19. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,381
  20. Allbee

    Allbee

    Joined:
    Feb 10, 2020
    Posts:
    4
    Sorry to be reviving an old thread. And I submitted my replies, either way through the form.
    But I have a question - why can we not record the sorting group sort order to the Mecanim?
    We can record sprite renderer's sort order, but not the sorting group.
    Kinda defeats the purpose of it, for me.
     
  21. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    Maybe too late, but...

    1. Yes, significantly.
    2. We use it to control grouped units of sprite renderers on multiple children that should always be displayed together. The problem is that a flat stack of individual layers and layer IDs for the whole scene is difficult to expand. If I want to add a child to a compound object and couldn't use a sorting group, I'd have to push up the sorting IDs of everything in the scene that I want to render after the added renderer.
    3. Yes, significantly.
    4. We have prefabs that can be put "inside" other prefabs with drag and drop. The prefabs can be organized in their own group, dropped onto another a group and parented, and then belong to the parent group until removed. We have a "chest," for example, into which we might put items. We can then move the chest, and the items move and draw with it as if they were always grouped. Then, by removing the child, they can be moved and drawn separately again.
    5. It would be a disaster if we couldn't use nested Sorting Groups without an equivalent replacement.

    That said, I would really like the ability to exclude individual Sprite Renderers from a sorting group hierarchy, to decouple their movement in a hierarchy from their rendering order.
     
  22. yyylny

    yyylny

    Joined:
    Sep 19, 2015
    Posts:
    92
    Sorting Groups is one of my favorite features of Unity and it's definitely essential to my game. My game uses many characters that are built by stacking multiple sprites, one on top of the other, so I was very happy to find out I can use Sorting Groups to put one multi-sprites character in front of the other without any visual issues. I was even more surprised when I noticed I can use many characters, with sorting groups applied to them, without any serious performance loss and it all worked flawlessly. Thank you for that feature!

    As for nested sorting groups, I've never needed them but it sounds like a very good feature that I might need some day.
     
  23. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,508
    Sorting Group is very useful in its own context, but I am also interested to find out how it affects drawcall batching.
    I am currently unable to batch my sprite drawcalls using URP and 2DRenderer and using Sorting Group. Also using Y as sorting axis.

    Any information in regarding to this? My sprites are ofcourse in atlas using the same shader created using Shader Graph.

    Is sorting order or sorting group component supposed to affect drawcall batching at all?
     
  24. rob_unity519

    rob_unity519

    Joined:
    Mar 26, 2019
    Posts:
    3
    I was using a package from the asset store to generate 2D shapes using splines (the output being a mesh and MeshRenderer). The spline shape component had fed sorting layer information to the MeshRenderer, but I was still running into sorting issues. For whatever reason, the spline shape would only sort using its pivot point if the Sorting Group component was added to the GO. Not sure why, as the spline shape contained the same fields.
     
  25. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    162
    My particle shells have problems with the sorting layer, because it's inside the character, hoping to have an option that isn't affected by the parent sorting layer.
     
  26. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737
    This would be amazing. Right now I have to unparent particle systems and sometimes even unparent and update their position every frame because of this.
     
    DungDajHjep likes this.
  27. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    204
    I really really need this for 3D for the exact same reason it was made for 2D.
    I have a set of renderers that must be renderer in order, no matter where they are. They act as one entity.
    I have a fire particle effect that I want to always render behind the sword that emits it, so each sword+fire pack should be be considered as a group. If I have several of this groups, they must be rendered correctly one in front of the other.
    The setting "Sorting fudge" on particle systems tries to help, but fails with objects that are close to each other. Stencil shader tricks are difficult to pull off. The idea would be to actually treat it as a group so that it gets rendered in the correct order.

    Please!! This would be awesome for 3D too!

    Also, right now it is impossible to do something like this with plain C# unless you write your own renderer culling and sorting. That's mostly not a good idea, nor possible with the current CullingResults API.
     
    Last edited: Feb 16, 2021
  28. ryanzec

    ryanzec

    Joined:
    Jun 10, 2008
    Posts:
    696
    1. Yes
    2. I need to be able to sort character part along with weapons but treat the entire character as 1 sorting layer when dealing with environment
    3. No
    4. N/A
    5. While I can't think of a need for them now, if I did have a needed and this was unavailable, I imagine it would require a bit (or a lot) of custom code to accomplish a similar effect
     
  29. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    1 - yes
    2 - to make sprite mask apply only to sprites within a given sorting group
    3 - no but I would if I could input a sorting group instead of a sprite in sprite mask. ( i want to add more masks on top of an already masked sprite, but i guess this would be extending the features of sprite mask and not sorting group )
    4-
     
  30. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    162
    bumb
     
  31. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, I assume that you would like a Renderer that is under the Sorting Group's hierarchy but wants to opt out of having the sorting credentials of the Sorting Group (the position and sorting order/layer of the Sorting Group)? If you have visual examples, that would be great!
     
    DungDajHjep likes this.
  32. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    162
    You have a Gameobject Player Character and a sortingGroup (character-order 0) on it. What do you think if you want to make some gameobject UI on top of player char? I'll insert it into the Gameobject Player Character, that's the fastest way. Here is a small and easier to understand example.

    I think a possible option on SortingGroup is: not affected by the parent SortingGroup.
     
    MousePods likes this.
  33. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737
    I agree with this 100%. I am doing a lot of extra work because of this.
     
  34. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    I still do not fully understand the use-cases yet so far. Would it be possible to provide more details ,and show some screenshots of your GameObject organisation in the Scene Hierarchy for your sorting and how they should look like in the Scene/GameView? Thanks!
     
  35. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737
    Here is just one example I have.'

    Sorting Layers:

    Coin Particle System
    Terrain
    Crown

    Both of these crowns are on the sorting layer Crown. I have a particle system coin under the rich crown. It needs to sort behind the terrain sorting layer, but I need the rich crown to sort on the Crown layer (in front of the terrain).

    Since having the particle system coin under the rich crown, it will now sort to the crown layer and is now above the terrain. I have to unparent the coin particle system now. It would be simpler and less complex to put a sorting group on the coin particle system and say, ignore parent sorting groups.

    Screen Shot 2021-03-26 at 9.40.16 AM.png
    Screen Shot 2021-03-26 at 9.41.04 AM.png
    Screen Shot 2021-03-26 at 9.41.52 AM.png

    If you need more examples lmk!

    Thanks!
     
  36. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Thanks for the example!

    If you have more examples, that would be great! This would help us create a more holistic solution for your use-cases!
     
  37. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737
    Sorting Layers:

    Rope
    Terrain
    Crown

    When the player jumps onto a rope, if the player is wearing a crown, the crown will sort behind the terrain because the rope has a sorting group, and I have to parent the player to the rope. When the player leaves the rope, then the crown will sort in front of the terrain again.

    My solution is to turn off the rope's sorting group when the player enters. This works except for when a rope potentially overlaps another rope.


    Screen Shot 2021-03-31 at 4.15.50 PM.png
     
  38. WitheringTreant

    WitheringTreant

    Joined:
    May 13, 2018
    Posts:
    4
    1. Are you using the Sorting Group component?
    2. If so, how are you using it? What problem are you solving using Sorting Groups?
    3. Are you using nested Sorting Groups?
    4. If so, how are you using them? What problem are you solving using nested Sorting Groups?
    5. If we removed the ability to nest Sorting Groups, how would this impact your productions?

    1. Yes
    2. I am using it in complex unit gameobjects. It helps me organize individual sprite renderers in complex hierarchies.
    3. I am using some nested sorting groups, yes.
    4. Again, it simplifies the organization of individual sprite renderers.
    5. Nested sorting groups are very important in my case of a 3D game using 2D sprites, with custom positional sorting. Without them, I would have to micromanage all my positional sorters.
     
  39. AHAKuo

    AHAKuo

    Joined:
    Apr 15, 2019
    Posts:
    5
    I was using it, but I realized it broke after I added more than 31-32 renderers. Even when nested, it didn't help.

    I ended up making a custom sorting system that's working just as fine and it's never breaking.
     
    DungDajHjep likes this.
  40. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    162
    please let me know if there is a temporary script that solves this problem.
     
  41. DungDajHjep

    DungDajHjep

    Joined:
    Mar 25, 2015
    Posts:
    162
    can you share it, still interesting
     
  42. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    bit late to the party, I'm using this a bit in my new game. Would be nice to have a list of "exclude from sorting group" that we can drag renderers into.
     
  43. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737
    @ChuanXin

    Just wondering if the override feature is on the roadmap and coming soon :)!
     
  44. FreshYeet

    FreshYeet

    Joined:
    Oct 10, 2015
    Posts:
    12
    Can you just make it so that I can add sprites to my character using the same depth that the sprite editor uses. Any attached sprites separate from the Rigged Character just is sits in-front. Unless there is a way to do this already please let me know. Nobody answers my questions
     
  45. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    This is on the 2D roadmap and is currently marked for 2022.1.
     
    MousePods likes this.
  46. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737