- Home /
Do Mesh Renderers for textures on quads have a sorting layer for 2D games?
I have a parallax background made of several layers that parallax at different rates. The textures that contain transparency are somehow rendering over my sprite renderer and I don't know how to change the priority.
The order with which objects are drawn to the screen is a combination of their sorting layer and order, render queue, and distance from the camera (i e. z coordinate). Opaque objects are drawn first and sorted front-to-back and then transparent objects are drawn but sorted back-to-front. Balancing these variables to get the correct layering can be tricky when you have a complex scene. What shader and material properties are you using? Is your character actually closer to the camera than the background (it appears so from the screenshot).
The Z coordinate of the background is behind the character. Background: 1, Character: 0, and Camera: -10. I am using custom sorting layers on the sprite renderer component because I have things like lights that must be in front of the character and climbable objects that must be behind the character. I can't seem to find any of the sorting layers on the mesh renderer for quads which is what I'm using to display the background.
I'm using an unlit texture shader for the sky layer, and an unlit transparent shader for the shubbery.
Answer by ghostravenstorm · Oct 30, 2016 at 09:37 AM
To answer my own question, yes, they do.
After digging around in the API I found a hidden variable for the Mesh Renderer. sortLayerName
. Well, not really hidden since its documented, but its not listed in the component interface in the editor. Bit of an oversight when you have sprites with their own render priority on top of a quad that automatically assumes the default layer.
public string sortingLayer;
void Awake(){
for(int i = 0; i < SortingLayer.layers.Length; i++){
if(SortingLayer.layers[i].name == sortingLayer)
this.GetComponent<Renderer>().sortingLayerName = sortingLayer;
}
}
This fixes the issue at runtime, but what about in editor? Is there a way to set that variable on change in the editor when working on a scene?