Search Unity

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

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

  1. rustum


    Unity Technologies

    Feb 14, 2015
    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.

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


    Feb 29, 2016
    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


    Jan 11, 2013
    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


    Jul 28, 2015
    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 :)
    rustum likes this.
  5. DanielThomas


    Mar 30, 2013
    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.
    rustum likes this.
  6. eightpoundgorilla


    Jan 24, 2014
    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. scofficial


    Apr 20, 2019
    Hey! I just started using after deleting my soundcloud account with the help of delete soundcloud account guide. will let you know the concerns gradually! thankyou
    Last edited: Nov 15, 2019
    rustum likes this.
  8. Th0re


    Sep 4, 2015
    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.
  9. tiddlesworthgames


    Dec 8, 2017
    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.
  10. Th0re


    Sep 4, 2015
    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
  11. ColbySg89


    Mar 26, 2015
    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.
  12. Th0re


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


    Dec 11, 2014
    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.
  14. dotsquid


    Aug 11, 2016
    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.
    orchard800 likes this.
  15. mgear


    Aug 3, 2010
    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..)
    2. never heard
    3. -
    4. -