< Return to Video

Sorting Layers - Official Unity Tutorial

  • 0:01 - 0:04
    When making a 2D game detailed control over the
  • 0:04 - 0:07
    order in which items are rendered is a necessity.
  • 0:08 - 0:10
    One way of doing this is by using the
  • 0:10 - 0:13
    distance of the sprite from the camera.
  • 0:13 - 0:15
    Unity however provides a much more
  • 0:15 - 0:17
    powerful tool to do this,
  • 0:18 - 0:19
    sorting layers.
  • 0:20 - 0:22
    Sorting layers work with the sprite renderer
  • 0:22 - 0:25
    to determine the rendering order of sprites
  • 0:25 - 0:27
    regardless of their distance from the camera.
  • 0:28 - 0:30
    A sprite, or group of sprites, can be
  • 0:30 - 0:32
    assigned to a sorting layer.
  • 0:33 - 0:36
    These sorting layers are given a sorting order.
  • 0:36 - 0:39
    The items in the sorting layer are rendered
  • 0:39 - 0:41
    regardless of distance to the camera
  • 0:41 - 0:43
    based on this sorting order.
  • 0:44 - 0:46
    It is worthwhile noting that the sprites
  • 0:46 - 0:49
    must still be within the camera's frustum to be rendered.
  • 0:49 - 0:52
    For even more control, individual sprites
  • 0:52 - 0:54
    can be assigned an Order In Layer value
  • 0:54 - 0:56
    to define the rendering order of sprites
  • 0:56 - 0:58
    within a sorting layer.
  • 0:59 - 1:01
    Sorting layers can be found in the
  • 1:01 - 1:03
    Tags and Layers Settings panel.
  • 1:04 - 1:06
    The tags and layers settings panel can be
  • 1:06 - 1:08
    accessed in 2 ways.
  • 1:09 - 1:11
    From the sprite renderer component select
  • 1:11 - 1:15
    Sorting Layer - Add Sorting Layer.
  • 1:15 - 1:17
    From anywhere within the editor select
  • 1:17 - 1:22
    Edit - Project Settings - Tags And Layers.
  • 1:23 - 1:25
    With the tags and layers panel open
  • 1:25 - 1:28
    sorting layers can be added, deleted or rearranged.
  • 1:29 - 1:31
    There will always be a default layer
  • 1:31 - 1:33
    present in the sorting layers list.
  • 1:34 - 1:37
    The default layer cannot be renamed or removed
  • 1:37 - 1:39
    but it can be rearranged.
  • 1:40 - 1:43
    To add a new sorting layer click the plus button.
  • 1:43 - 1:45
    To rename a sorting layer select
  • 1:45 - 1:48
    the Name field and enter a new name.
  • 1:49 - 1:51
    To rearrange the order of a sorting layer
  • 1:51 - 1:54
    click and drag the layer in to a new position.
  • 1:55 - 1:57
    The sorting layers will render in
  • 1:57 - 1:58
    list order from top to bottom
  • 1:58 - 2:00
    with the first item drawn at the back
  • 2:00 - 2:04
    and the last item drawn on top as the last layer.
  • 2:05 - 2:07
    To assign a sprite to a sorting layer
  • 2:07 - 2:09
    select the sprite to be assigned
  • 2:09 - 2:11
    and in the sprite renderer component
  • 2:11 - 2:13
    use the Sorting Layer drop-down list
  • 2:13 - 2:15
    to choose a sorting layer.
  • 2:16 - 2:18
    For detailed control over multiple
  • 2:18 - 2:20
    sprites on the same sorting layer the
  • 2:20 - 2:22
    Order In Layer value can be set.
  • 2:23 - 2:25
    If, for example, multiple sprites
  • 2:25 - 2:27
    are used to create a character
  • 2:27 - 2:29
    all the sprites used to create that character
  • 2:29 - 2:31
    could be assigned to one sorting layer.
  • 2:32 - 2:34
    And then within that sorting layer
  • 2:34 - 2:36
    Order In Layer can be used to determine
  • 2:36 - 2:39
    the draw order of the sprites, creating the character.
  • 2:41 - 2:43
    It is worth noting that sorting layers
  • 2:43 - 2:45
    and Order In Layer are global and available
  • 2:45 - 2:47
    to all renderers in Unity.
  • 2:48 - 2:50
    Every renderer, including particle renderers
  • 2:50 - 2:52
    can specify a sorting layer and use a
  • 2:52 - 2:55
    Order In Layer value, even though only the sprite
  • 2:55 - 2:59
    renderer exposes these properties in the inspector.
  • 2:59 - 3:02
    To use sorting layers and Order In Layer with
  • 3:02 - 3:04
    other renderers simply use a script
  • 3:04 - 3:06
    to assign these values.
  • 3:07 - 3:09
    It is possible to set the sorting layer
  • 3:09 - 3:13
    by name or ID with renderer.sortingLayerName.
  • 3:14 - 3:17
    Or renderer.sortingLayerID.
  • 3:18 - 3:20
    To set the Order In Layer property use
  • 3:20 - 3:23
    renderer.sortingOrder.
  • 3:23 - 3:26
    For more information on renderers, tags and layers
  • 3:26 - 3:29
    including sorting layers and the Order In Layer
  • 3:29 - 3:32
    please see the information linked below.
Title:
Sorting Layers - Official Unity Tutorial
Description:

more » « less
Video Language:
English
Duration:
03:44

Spanish subtitles

Revisions