Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity 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:
    110
    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:
    17
    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 :)
     
    SiriAnimates and rustum like this.
  5. DanielThomas

    DanielThomas

    Joined:
    Mar 30, 2013
    Posts:
    73
    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:
    34
    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:
    34
    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:
    34
    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:
    824
    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:
    181
    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:
    6,299
    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:
    364
    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:
    3,850
    No, what's that?
     
  18. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,300
    APSchmidt likes this.
  19. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,850
  20. Allbee

    Allbee

    Joined:
    Feb 10, 2020
    Posts:
    5
    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,232
    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:
    32
    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:
    1,915
    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:
    72
    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:
    169
    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.
unityunity