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:
Background
and Foreground
. From there, bring the Background
layer up (by dragging the = symbol on the left up) so that it's drawn first:Now, the background will be drawn first, then the Default
object (acting as our middleground), then the foreground.
Background
.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:
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.