Version: 2023.1
Set up sprites
Sprite Renderer

Sort sprites

Unity sorts renderers by several criteria, such as their Layer order or their distance from the Camera. Unity’s Graphics settings (menu: Edit > Project Settings > Graphics) provide a setting called Transparency Sort Mode, which you can use to control how to sort sprites depending on their position in relation to the Camera. More specifically, it uses the sprite’s position on an axis to determine which ones are transparent compared to others.

An example of when you might use this setting is to sort Sprites along the y-axis. This is quite common in 2D games, where sprites that are higher up the y-axis are sorted behind sprites that are lower, to make them appear further away.

There are four Transparency Sort Mode options available:

Sort Mode 描述
Default Sorts based on whether the Camera’s Projection mode is set to Perspective or Orthographic.
Perspective Sorts based on perspective view. Perspective view sorts sprites based on the distance from the Camera’s position to the sprite’s center.
Orthographic Sorts based on orthographic view. Orthographic view sorts sprites based on the distance along the view direction.
Custom Axis Sorts based on the given axis set in Transparency Sort Axis.

If you set the Transparency Sort Mode to Custom Axis, you also need to set the Transparency Sort Axis.

If the Transparency Sort Mode is set to Custom Axis, renderers in the Scene view are sorted based on the distance of this axis from the camera. Use a value between –1 and 1 to define the axis. For example: X=0, Y=1, Z=0 sets the axis direction to up. X=1, Y=1, Z=0 sets the axis to a diagonal direction between X and Y.

For example, if you want sprites to behave like the ones in the image above, set the Transparency Sort Mode to Custom Axis, and set the Y value for the Transparency Sort Axis to a value higher than 0.

Sort sprites using scripts

You can also sort sprites per camera through scripts, by modifying the following properties in Camera:

例如:

var camera = GetComponent<Camera>();

camera.transparencySortMode = TransparencySortMode.CustomAxis;

camera.transparencySortAxis = new Vector3(0.0f, 1.0f, 0.0f);
Set up sprites
Sprite Renderer