I am confused by render queue and sorting layer.
Any one can shade light on the difference between the two concept?
1 Answer
1Render Queue is at the shader level while sorting layer is at a higher level, developed specifically for Sprites but open for use by any renderer. The concepts I suppose are similar in that you are grouping together and ordering renderable draw calls, but since you may have batched sprites (or other renderers) that use the same material (and thus the same render queue), you may need an extra “sorting” capability to determine the priority of drawing sprites from a single texture.
Render Queue is the lowest level sort applying to everything in the scene, while Sorting Layer and then Order in Layer provide an extra sub-sort specifically for Sprites.
Sorting layers were developed for Sprites to have the ability to place one sprite over another, but you can apply them to your own renderer as well:
https://github.com/nickgravelyn/UnityToolbag/tree/master/Sorting%20Layer
If you had two sprites that used two different materials, and the render queue on those materials differed, but the sorting layer was the same, you may get unexpected results since the render queue would take priority and override the Order in Layer. But I can’t confirm that.
I think you are right, except that sorting layer is not limited in sprite, all renderer has sorting layer. After some testing, I find that sorting layer only works for objects in the same render queue. It means you can using sorting layer to sort rendering order of sprite and particle system, because both sprite and particle shader has the same "Transparent" render queue.
– ofusionThat is correct, but only the Sprites (currently) expose it in the inspector, meaning to me this is how Unity intended it to be used. But it is a handy feature, so I will update my post to correct that. I was trying to think of a good reason to use it, and you just pointed it out. That would be very handy indeed if you wanted to mix 2D sprites with other engine features like particles so the particles are always on top.
– supernat