- Home /
Need more flexible sorting layers
I am creating an 2D tile system that basically uses voxels to store tile information. A chunk holds tile and layer information. Each chunk in the z axis is a new "world layer" (my own term) like underground or the second story of a house. My problem comes from the fact I want to just define the chunk layers in unity like ground, foliage, walls etc. Then have the world layers dealt with at runtime so everything on a higher world layer renders above the lower. From what I can tell changing the unity layers at runtime is not possible and adding layers by hand for each possible world layer is just silly.
I could use z depth to determine what is visible but that would mean throwing away the sprite renderer and I really don't want to do that. Looking into Material.renderQueue seems promising but I am not sure exactly how it works and its relationship with with sortin layer.
This one has me a bit stumped and I am not sure if it is even feasible to use the sprite renderer here.
Answer by Pyrian · Jul 17, 2014 at 10:40 PM
You can use Z-values and the camera's Z-value to exclude Sprites from rendering just fine. It won't control the order they render - you need SortingLayer for that - but it can prevent them from rendering at all.
So, if you use SortingLayer/SortingOrder and corresponding Z-values to design your world, you can scroll "down" in it by setting the camera's Z-value.
If I am understanding you correctly that would mean that I could no longer show more than one world layer at a time. I would like to show the second story of a building for instance.
No, you just don't see anything "above" your camera. You can still see multiple z-values deep, if you want. You can even cap that depth in the camera settings (it defaults to 1000).
Hmmm maybe I am just being thick but I am not seeing how this solves the issue. It seems a sprite with a higher sorting layer will always be on top of the lower no matter the z value. Only when the sorting layers are the same will the z value be taken into account. When I stack groups of layers (my world layers) I can set the z value of the chunk but it only matters on the matching layers. So even if I place a piece of foliage on a lower chunk it will always be above the ground of a higher chunk. Hiding chunks on a world layer I dont need is trivial but it does not really help when I need to display multiple chunk layers.
Then I don't think I understand your problem at all. You can set what appears on top of what, and you can set what doesn't appear at all. What is it that you cannot figure out how to do?
Would it help if I mentioned that you can change what order Sprites are rendered in (i.e. which ones are on top) during runtime by editing renderer.SortingOrder (and/or renderer.SortingLayer and/or renderer.SortingLayerID)?
Your answer
Follow this Question
Related Questions
[2D] [C#] Sprite disappears after changing position 1 Answer
Render particles in front of any sort layer (2D sprites) 3 Answers
Unity 5: MeshRenderer and SortingLayer not working? 6 Answers
Why do my GameObjects have a flickering line above them? 2 Answers
Why do my hinge joints 2d not work when they are first loaded? 1 Answer