How to Use Sorting Layers in Unity
I wanted to learn more about how Sorting Layers work in Unity. I make assets for 3D projects, so I was unfamiliar with Unity's 2D Pipeline.
I spent a few hours researching the Sorting Layers system. Then, I put together this comprehensive guide to Sorting Layers. I designed this guide for beginner game developers with Unity. But, you can still learn something new if you are an intermediate or advanced game developer.
This article is the most comprehensive summary of the Sorting Layers system. It is the most complete explanation of how Sorting Layers relate to the tags and layers system.
I hope you find it valuable!
:::note Key Takeaways
Sorting Layers are a rendering option in the 2D Pipeline.
To make Unity render an object on top, give that renderer a higher-priority Sorting Layer.
Unity renders layers at the bottom of the list in front of layers at the top.
To set the Sorting Layers for your project, open Edit > Project Settings > Tags and Layers.
To set the Sorting Layer for a specific Renderer, open it in the Inspector.
With the high-level overview done, let's get into the details.
What are Sorting Layers?
The Sorting Layer is a rendering option in Unity. When you assign an object to a Sorting Layer, you tell Unity when it should render that object.
Why does rendering order matter?
The rendering order sets which objects Unity will render on top of others. Think of it like painting. You start with a blank canvas. Then, you apply some brush strokes. Then, you paint on top of those brush strokes. This technique is how rendering works, too.
Do I need to use Sorting Layers?
No. Unity has an entire sorting pipeline. You don't need to use Sorting Layers. You should build your project and allow Unity to handle sorting based on camera distance. Then, you can override the sorting layer if you need something special.
Where can I find the Sorting Layers?
You can set the Sorting Layer for a specific Renderer in the Inspector.
In the 2D Pipeline, most renderers expose the Sorting Layer option.
In the 3D Pipeline, most renderers don't show this option.
When do you use Sorting Layers?
Sorting Layers enable you to set the render order of different sprites in your project. Unity only exposes Sorting Layers for 2D projects.
If you want certain sprites to render in front of others, the Sorting Layers system lets you do that.
How do Sorting Layers work?
Unity renders the Layers from top to bottom. As a result, layers at the top will appear behind layers at the bottom.
In this example, I set up three Sorting Layers: Background, Default, and Foreground.
Unity organizes Sorting Layers in a List. Unity renders Layer 0 first, then Layer 1, and then Layer 2.
In this example, Unity renders in this order:
I made a diagram to illustrate the concept.
The camera renders the Background. Then, the camera draws the Default layer on top of the Background. Then, the camera paints the Foreground on top of the Default layer.
As a result, anything in the Foreground looks like it is in front of the Default layer. And anything in the Default layer looks like it is in front of the Background layer.
Try playing around with sorting layers in a small test project. Practicing sorting layers will help you form an intuitive understanding. It is crucial that you intuitively understand this system in Unity.
How do I configure the Sorting Layers?
To open the Sorting Layers menu, click the 'Add Sorting Layer' option in the Sorting Layer dropdown. Unity will open the Tags and Layers asset in the Inspector.
You can also open the Sorting Layers by going to your Menu bar > Edit > Project Settings > Tags and Layers.
How can I sort all children of a game object?
Use the Sorting Group component. Unity handles the Sorting Layer per renderer. When you set a Sorting Layer for a parent, Unity does not apply that Sorting Layer to all children. Use the Sorting Group component to apply a sorting layer to all children. This Component overrides the sorting layer for all children of a game object.
To add this Component, go to the parent game object, then Add Component > Sorting Group.
Unity treats all the objects within a Sorting Group as a single renderer. So, be careful about overriding default sorting behavior.
How does the Sorting Layer work under the hood?
Unity’s ChuanXin explained how Unity’s Sorting Layer works. Unity sorts the renderers based on their Sorting Layer, then their Order In Layer. Then, it sorts using the other standard criteria. Then, it passes this sorted list to the renderer. At that point, the renderer takes over to handle batching.
How does Unity sort 2D objects?
Unity sorts 2D renderers using a fixed 2D Sorting algorithm.
- Sorting Layer
- Order in Layer
- Sorting Group
- Material settings
- Internal Tiebreaker
Can I set the Sorting Layer with code?
Yes. You can set the Sorting Layer API by ID or by name. Setting by name is more accessible.
I wrote a lightweight script to show this. The following script gets the Renderer component. Then, it sets the Sorting Layer to the "Foreground" Layer.
public class SetSortingLayer : MonoBehaviour
private void OnEnable()
Renderer renderer = GetComponent<Renderer>();
renderer.sortingLayerName = "Foreground";
What is the difference between Sorting Layer and Order in Layer?
Sorting Layer sorts first, Order in Layer sorts second. Unity will always draw a higher-priority Sorting Layer on top of a lower-priority one. This rule holds regardless of their respective Order in Layer setting. In other words, the Order in Layer sets priority within a specific Sorting Layer.
Is it better to use Sorting Layers or Order in Layer?
Use Sorting Layers to group and control the rendering for large groups of objects.
I will give an example. You could create one Sorting Layer for Foreground elements. Then, create one for midground elements. And finally, create one for background elements.
Then, you can use the Order In Layer option to control the render order within each Layer.
What is the difference between Sorting Layer and Layer?
The Layer is the Layer option at the top right of the Inspector. I like to refer to this as the Physics Layer to clarify things. The Physics Layer has no impact on sprite sorting. Use the Physics Layer for organizing and managing collisions and physics interactions. Use the Sorting Layer to control the render order and priority.
What is the difference between the Sorting Layer and the Rendering Layer Mask?
You use the Rendering Layer Mask with custom rendering commands. You can use this mask to filter renderers during manual render commands. Manual render filtering is an advanced use case. You use the Sorting Layer to set the rendering order in the default render pipeline workflow.
In conclusion, Sorting Layers are a powerful tool for controlling the render order of objects in your Unity project. By understanding how Sorting Layers work, you can create games that meet your project requirements.
If you want to level up, you can use Global Variables to store the names of your Sorting Layers. This makes it easier to reference your variables in code.
To really take it to the next level, you can use the OnValidate event to update the Sorting Layer of an object when its properties change. This will ensure that your objects are always rendered in the correct order.
By using Sorting Layers for 2D rendering, you can get the most out of your rendering pipeline. This will help you spend your time more effectively and build the best game possible.
65+ Tips: How to improve a Unity Game's Performance
65+ tips to help you improve your Unity game's performance.
The basics of arrays in C#. Declaring, initializing, assigning values, accessing elements, and using foreach loops.
Low Poly Trees
This article outlined the world of low poly trees. You learned how to make low poly trees. You also learned where to use low poly trees.