Version: 2020.3
Language : English
Gameplay in 2D
Work with sprites

2D Sorting

Overview

Unity sorts Renderers according to a priority order that depends on their types and usages. You can specify the render order of Renderers through their Render Queue. In general, there are two main queues: the Opaque queue and the Transparent queue. 2D Renderers are mainly within the Transparent queue, and include the Sprite RendererA component that lets you display images as Sprites for use in both 2D and 3D scenes. More info
See in Glossary
, Tilemap Renderer, and Sprite Shape Renderer types.

Transparent Queue Sorting Order by Priority

2D Renderers within the Transparent Queue generally follow the priority order below:

  1. Sorting Layer and Order in Layer
  2. Specify Render Queue
  3. Distance to Camera
  4. Sorting Group
  5. Material/Shader
  6. A Tiebreaker occurs when multiple Renderers have the same sorting priorities.

There are other factors which can cause the sorting order to differ from the regular priority order. These factors vary from project to project.

Sorting Layer and Order in Layer

The Sorting Layer and Order in Layer (in the Renderer’s Property settings) are available to all 2D Renderers through the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary
window or via the Unity Scripting API. Set the Renderer to an existing Sorting Layer or create a new one to determine its priority in the renderingThe process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. More info
See in Glossary
queue. Change the value of the Order in Layer to set the Renderer’s priority among other Renderers within the same Sorting Layer.

Specify Render Queue

You can specify the Render Queue type of the Renderer in its Material settings or in the ShaderA program that runs on the GPU. More info
See in Glossary
settings of its Material. This is useful for grouping and sorting Renderers which are using different Materials. Refer to documentation on ShaderLab: SubShader Tags for more details.

Distance to Camera

The Camera component sorts Renderers based on its Projection setting. The two options are Perspective and Orthographic.

Perspective

In this mode, the sorting distance of a Renderer is the direct distance of the Renderer from the CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary
’s position.

Orthographic

The sorting distance of a Renderer is the distance between the position of the Renderer and the Camera along the Camera’s view direction. For the default 2D setting, this is along the (0, 0, 1) axis.

When you set the Camera component to Perspective or Orthographic, Unity automatically sets the Camera’s TransparencySortMode to match the selected mode. You can set the Transparency Sort Mode manually in two ways:

  • Open the Project SettingsA broad collection of settings which allow you to configure how Physics, Audio, Networking, Graphics, Input and many other areas of your project behave. More info
    See in Glossary
    and go to Graphics, then under Camera Settings use Transparent Sort Mode
  • Set the Camera’s TransparencySortMode via the Scripting API.

The Camera Transparency Sort Mode settings are under the Graphics category in the Project Settings (main menu: Edit > Project Settings > Graphics). When this is set to Default, a Camera component’s Projection setting take priority. When this is set to an option other than Default, the Camera component’s Projection setting remains the same, but the Camera’s Transparency Sort Mode changes to that option.

An additional option available through the Project settings and via the Scripting API is the Custom Axis sort mode.

Custom Axis sort mode

Select this mode to sort Renderers based on their distance along the custom axis you set in the Project settings (main menu: Edit > Project Settings > Graphics > Transparency Sort Axis). This is commonly used in projects with Isometric Tilemaps to sort and render the Tile Sprites correctly on the Tilemap. Refer to Creating an Isometric Tilemap for further information.

Sprite Sort Point

By default, a SpriteA 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary
’s Sort Point is set to its Center, and Unity measures the distance between the camera’s Transform position and the Center of the Sprite to determine their render order during sorting. An alternate option is to set a Sprite’s Sort Point to its Pivot position in World Space. Select the Pivot option in the Sprite’s Sprite Renderer property settings and edit the Sprite’s Pivot position in the Sprite Editor.

Sorting Group

The Sorting Group is a component that groups Renderers which share a common root together for sorting purposes. All Renderers within the same Sorting Group share the same Sorting Layer, Order in Layer, and Distance to Camera. Refer to documentation on the Sorting Group component and its settings for more details.

Material/Shader

Unity sorts Renderers with the same Material settings together for more efficient rendering performance, such as with dynamic batchingAn automatic Unity process which attempts to render multiple meshes as if they were a single mesh for optimized graphics performance. The technique transforms all of the GameObject vertices on the CPU and groups many similar vertices together. More info
See in Glossary
.

Tiebreaker

When multiple Renderers have identical sorting priority, the tiebreaker is the order that Unity places the Renderers in the Render Queue. Because this is an internal process that you have no control over, you should use the sorting options (such as Sorting Layers and Sorting Groups) to make sure all Renderers have distinct sorting priorities.

Gameplay in 2D
Work with sprites
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961