Sorting Layers

Sorting Layers to Control Sprite Rendering Order

Unity Video on Sorting Layers

Sorting Layers provide control over layer ordering of sprite rendering. The animation below shows how to add a sprite to function as a background image. Next, on the Sprite Component for that background image, select the SortingLayer dropdown, choose to create a Sorting Layer. Start by creating 2 Sorting Layers (we'll create additional layers for more complicated games). The order of these items in the Sorting Layer list determines the relative render ordering of gameObjects. Items higher in the list: Background, are rendered first, with items lower on the list being rendered later, or closer to the viewer. Order in Layer Within a given Sorting Layer, more granular control can be added by using the Order in Layer Option of the Sprite Renderer Component. Items with a higher value for Order in Layer are rendered in front of objects with lower Order in Layer values, so higher valued objects appear closer to the camera.

Create and Apply Sorting Layers

Sprites rendered first appear behind sprites that are rendered later in the order. 1. Select the layer dropdown tab from the top of the inspector panel and then we choose to add new layers. 2. Select to add new sorting layers, create a Background, Scenery, Player, Spawned, UI, sorting layers. 3. The ordering of the layers in this list defines the ordering of layers in our scene. 4. For each GameObject that we assign with a Sorting Layer Background those objects will be behind the GameObjects that have been assigned to the foreground Layers. 5. Order in Layer gives control over rendering within a Sorting Layer, higher numbers are rendered later so they are closer to the viewer 6. Transform values for an object can also determine sprite visibility layering for objects on the same sorting layer. 7. The Main Camera has a Transform position vector of (0,0,-10) by default. This means that to move objects closer to the camera, can give them a Z transform value that is between 0 and -10.

Sorting Groups

Sorting Groups allow Sorting Layers to be added to any Hierarchy of GameObjects, by default, sorting layers are already available for default hierarchy objects. The canvas provides an example of a hierarchy that does not have sorting layers for child objects by default, this can be changed by adding a Sorting Group component to the parent Canvas gameObject.

Last updated