This article is obsolete now. Unity got many new features and it is possible to set SortingLayer now from many places.
In Unity3D 4.3 version there was a long-awaited 2D framework, which brought with itself the work mechanism with sprites, for display of which “Sprite Renderer” is responsible. There is a set of tutorial and video in a network how to work with sprites, I just want to stop on two important settings which are in “Sprite Renderer”, and on a use problem of 3D objects of Unity in 2D games.
Important settings are: “Sorting Layer” and “Order in Layer”:
Why these two parameters are necessary? In a nutshell in 2D framework z-coordinate isn’t used, it doesn’t effect on displaying order objects in a scene. For displaying order other mechanism is responsible – it’s mechanism of layers. It’s possible to operate displaying order of sprites in a scene by appointment of a certain layer (“Sorting Layer”) to sprite, and also, if necessary, displaying order internally of a concrete layer can be adjusted by means of “Order in Layer” parameter.
Displaying order of layers in a scene is adjusted in a window of editing layers:
But Unity3D comprises the mass of interesting functionality, which was made for 3D and which wants to be used in 2D. For example, particles system. Let’s say, in 2D scene you want to use effects from particles system and place one of effects on 2D scene. Here you receive a problem of incorrect displaying of effect (it can be expressed in different ways – partial displaying of effect, blinking, full invisibility and another). Manipulations with z-coordinate won’t lead to necessary result as at 2D displaying of scenes z-coordinate doesn’t effect on displaying order.
This problem is easily solved. “Sorting Layer” and “Order in Layer” properties were added not to class “SpriteRenderer”, but tо basic class “Renderer”, which other classes are inherited, and which are responsible for displaying, but as settings were taken out only for class “SpriteRenderer”, and in others aren’t available. Therefore, we need to write own component which will establish a layer and an order in layer for any renderer.
For myself I wrote such component which can be taken according to links: SortingLayerSettings (a component) and SortingLayerSettingsEditor (editor extension for parameters settings).
Added both scripts to the project, it is possible to add SortingLayerSettings component on any object and to adjust a displaying order of object in 2D scene. For example for “3D Text”:
And it is easy to operate displaying order:
Thus, we can easily use Unity3D objects in 2D scenes. Mechanism perfectly works! On personal experience I used system of particles and 3D text in several projects. I suppose with other types of objects there will be no problems.
Practically, SortingLayerSettings component doesn’t make sense after we specified the necessary layer for object. But at the time of application development there is a need in it, as it can be necessary in operating time of game to experiment with layers or to see, for which layer this or that object belong. But during game release there is a sense to remove all “SortingLayerSettings” from all objects.