- I have a sprite in the center of my screen.
- The sprite is in world space, but sometimes it is obscured.
Occasionally, the sprite will collide with objects that it gets too close to and will be obscured.
There are several options for rendering a sprite in front of everything else in your scene.
Firstly, you can use two cameras: one for UI and another one for the "normal" scene.
Furthermore, you could use a shader that has a ZTest of 'always' (SL-CullAndDepth). Download the Unity shader source by clicking the Downloads drop-down > Built in shaders, and add that ZTest statement to the sprite shader.
- You can find the source for all Unity built-in shaders at the Unity download archive. Make sure you download the built in shaders for the version Unity version you are using.
Another option is to put your UI on a canvas set to screen space - camera and then set the sorting layer to UI. You can learn about the different canvas render modes and some of their uses at the Unity UI Docs - Canvas
Finally, you can overwrite the material's render queue value using Material.renderQueue. The value for Overlay is 4000. Using this value should make the sprite render on top of everything.
For more details, please see the following pages from our manual:
Setting the render queue is particularly useful when dynamic meshes and procedural materials are used to affect sprites by sticking them to the camera; such as needed presently to create a pure ECS UI. In such cases, we're playing a game where we squeeze the best performance possible from our software, so using multiple cameras is out right off. We don't have access to the sprite shader, as the Sprite Renderer is decidedly not RenderMesh. And naturally, using a canvas is impossible because none exists for ECS.
Having unnecessarily rambled at length about that, recall that when your shader is changed, you will have to set your render queue again. It's easy to forget if you don't do it by habit.
Please sign in to leave a comment.