Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How does Sorting Layer work *under the hood*?

Discussion in '2D' started by kodra_dev, Nov 17, 2022.

  1. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    112
    I know how to use Sorting Layer and Order in Layer. But how do they actually work under the hood? How the render pipeline gets this information? Is there a "sprite sorter" to sort all the sprites before rendering every frame? Or are the mesh's Z changes? Or the sorting layer info is written to z-buffer for alpha blend?

    If SpriteRenderer doesn't exist, how could we implement our own "Sorting Layer"?
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Before rendering, all the renderers are sorted in order before they are rendered. For renderers in the Transparent Queue, they are sorted using the Sorting Layer and Order first before the other criteria (See https://docs.unity3d.com/Manual/2DSorting.html). The sorted renderer list is then passed to the render pipeline for batching and rendering. The sorted renderer order is not written to the Z-buffer.

    All renderers do have a Sorting Layer and Order in Layer, even if you may not see it in their respective Inspectors (See https://docs.unity3d.com/ScriptReference/Renderer-sortingOrder.html). You could use that to order your Renderer if you are not using a SpriteRenderer.
     
  3. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    112
    Thank you very much for the answer!

    Does it mean there is an O(NlogN) sorting that happens every frame tho?

    (I know it's probably not a big deal for most of the games... just curious if it might cause some performance hit)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,044
    Hmm, that sounds suspiciously like premature optimization to me!!!

    Also, Unity is a black box. Everything it does internally is unknowable and subject to change at any time.

    Even what the Unity representative said above might no longer be true in the future for any number of reasons.

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  5. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    yes be careful, its taboo to ask about optimization before running into performance problems(never mention performance if you dont bring all your logs of your issues)
     
    CesarzAlnb likes this.