askvity

How to Add Sorting Layer in Unity

Published in Unity Sorting Layers 4 mins read

Adding a Sorting Layer in Unity is essential for controlling the render order of 2D sprites and UI elements, ensuring they appear in front of or behind others correctly.

What are Sorting Layers?

Sorting Layers are a system in Unity that allows you to define distinct layers for 2D rendering. Unlike regular layers (which control physics and culling), Sorting Layers control the drawing order of renderers like SpriteRenderers and UI Canvas elements. Renderers on a higher Sorting Layer are drawn on top of renderers on a lower Sorting Layer. Within the same Sorting Layer, the Order in Layer property determines the rendering order.

How to Access the Sorting Layers Settings

There are two primary ways to access the Sorting Layers settings in Unity:

  1. Via a Renderer Component: This is often the quickest way when working with a SpriteRenderer or other 2D renderer.

    • Select a GameObject in your scene that has a SpriteRenderer component (or similar, like a TilemapRenderer).
    • In the Inspector window, locate the Sorting Layer dropdown property on the component.
    • Click the Sorting Layer dropdown.
    • Click the 'Add Sorting Layer' option in the Sorting Layer dropdown. This method, as mentioned in the reference, will open the Tags and Layers asset in the Inspector.
  2. Via Project Settings: You can access the settings directly from the Unity menu.

    • Go to your Menu bar: Edit.
    • Select Project Settings.
    • In the Project Settings window that appears, select the Tags and Layers category from the list on the left.

Both methods will open the Tags and Layers settings in the Inspector window, specifically showing the Sorting Layers section.

Adding a New Sorting Layer

Once you have the Tags and Layers settings open (via either method described above), you can add new layers:

  1. In the Inspector, locate the Sorting Layers section.
  2. You will see a list of existing sorting layers (initially, only 'Default').
  3. Below the list, click the + button.
  4. A new sorting layer will appear in the list, typically named "New Sorting Layer".
  5. Click on the name of the new layer to rename it (e.g., "Background", "Characters", "Foreground").

You can click the + button multiple times to add several layers. You can also reorder layers by dragging them up or down in the list using the handle on the left of each layer name. Layers higher in the list are drawn on top.

How to Assign a Renderer to a Sorting Layer

After you have defined your custom Sorting Layers in the Tags and Layers settings, you can assign any renderer (like a SpriteRenderer, TilemapRenderer, ParticleSystemRenderer, or Canvas) to one of these layers:

  1. Select the GameObject with the renderer component you want to assign.
  2. In the Inspector, find the renderer component (e.g., SpriteRenderer).
  3. Locate the Sorting Layer dropdown property.
  4. Click the dropdown and select the desired sorting layer you created from the list.
  5. You can also adjust the Order in Layer property (an integer) to control the drawing order of renderers within the same Sorting Layer. Higher numbers are drawn on top.

By utilizing Sorting Layers, you can effectively manage the visual depth and drawing order of your 2D elements in Unity scenes.

Related Articles