Working with sorting layers

Next, I want to ensure that the sprites that we added will actually be in the correct order from any of the other sprites that we add to the level. This is because when we place objects on the same Z position, we are telling the graphics card that we don't care what order the images are drawn to the screen (also known as z-fighting). In this case we do and even if it "looks right" in the editor right now, if you ever load or restart the scene it may not look correct so rather than wait for the problem I suggest we fix it now. I could change the Z position of the object, but rather that, we can make use of the sorting layers property instead:

  1. Select one of the background objects from the Hierarchy tab and then from the Inspector tab, go to the Sprite Renderer component and click on the Sorting Layer dropdown. Then, select Add Sorting Layer… click on the + icon two times and name the newly created layers Background and Foreground. From there, bring the Background layer up (by dragging the = symbol on the left up) so that it's drawn first:
    Working with sorting layers

    Now, the background will be drawn first, then the Default object (acting as our middleground), then the foreground.

  2. Of course, this isn't doing anything just yet, so select the two background objects again and then under the Sprite Renderer component change the Sorting Layer to Background.
  3. Afterwards, change the two groundGrass sprites to use the Foreground Sorting Layer. We'll keep the Plane object at the default so it'll stay as is.

At this point, the Sorting Layers are working correctly! If you want to see what happens when we change the order, go to the Add Sorting Layer… menu again (or click on Layers | Edit Layers from the top-right toolbar) and then drag and equals sign buttons in a different order. Note that if I bought Background all the way down, the following will happen:

Working with sorting layers

All of the other sprites would disappear because they'd be drawn first and then the background on top of them. Of course, change it back afterwards.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset