When making a 2D game detailed control over the
order in which items are rendered is a necessity.
One way of doing this is by using the
distance of the sprite from the camera.
Unity however provides a much more
powerful tool to do this,
sorting layers.
Sorting layers work with the sprite renderer
to determine the rendering order of sprites
regardless of their distance from the camera.
A sprite, or group of sprites, can be
assigned to a sorting layer.
These sorting layers are given a sorting order.
The items in the sorting layer are rendered
regardless of distance to the camera
based on this sorting order.
It is worthwhile noting that the sprites
must still be within the camera's frustum to be rendered.
For even more control, individual sprites
can be assigned an Order In Layer value
to define the rendering order of sprites
within a sorting layer.
Sorting layers can be found in the
Tags and Layers Settings panel.
The tags and layers settings panel can be
accessed in 2 ways.
From the sprite renderer component select
Sorting Layer - Add Sorting Layer.
From anywhere within the editor select
Edit - Project Settings - Tags And Layers.
With the tags and layers panel open
sorting layers can be added, deleted or rearranged.
There will always be a default layer
present in the sorting layers list.
The default layer cannot be renamed or removed
but it can be rearranged.
To add a new sorting layer click the plus button.
To rename a sorting layer select
the Name field and enter a new name.
To rearrange the order of a sorting layer
click and drag the layer in to a new position.
The sorting layers will render in
list order from top to bottom
with the first item drawn at the back
and the last item drawn on top as the last layer.
To assign a sprite to a sorting layer
select the sprite to be assigned
and in the sprite renderer component
use the Sorting Layer drop-down list
to choose a sorting layer.
For detailed control over multiple
sprites on the same sorting layer the
Order In Layer value can be set.
If, for example, multiple sprites
are used to create a character
all the sprites used to create that character
could be assigned to one sorting layer.
And then within that sorting layer
Order In Layer can be used to determine
the draw order of the sprites, creating the character.
It is worth noting that sorting layers
and Order In Layer are global and available
to all renderers in Unity.
Every renderer, including particle renderers
can specify a sorting layer and use a
Order In Layer value, even though only the sprite
renderer exposes these properties in the inspector.
To use sorting layers and Order In Layer with
other renderers simply use a script
to assign these values.
It is possible to set the sorting layer
by name or ID with renderer.sortingLayerName.
Or renderer.sortingLayerID.
To set the Order In Layer property use
renderer.sortingOrder.
For more information on renderers, tags and layers
including sorting layers and the Order In Layer
please see the information linked below.