Ok, so I’ve been having a couple of issues with 3D text in my 2D game. Mainly it boils down to setting the depth of the 3D text to be behind other objects instead of drawing over the top. I’ve changed the shader to match the one that I’ve seen linked around on the unity forums and it sort of works and sort of doesnt. When I created a test scene with just one cube and some 3d text it worked perfectly with the text being hidden behind the cube, however in my actual game scene (when I apply the same font & text material) it doesn’t work the same way.
The only reason I can see this happening is because I’m using sprites in my main game and was using a cube in my test scene. Maybe there’s an issue with the sorting depth interfering with how things are drawn? I am very stuck on this issue and I’d love any help.
I also read somewhere that non-sprite objects were sent to the default sorting layer, so this might be what’s causing the issue? But I still wouldn’t know how to fix it if this were the case.
Here are some pictures to illustrate the issue:
This one is working exactly as it should (this is the text scene). The text is behind the object since it is further away from the camera.
However, in the actual game scene the text for the behind clipboard is being rendered on top of the above clipboard, even though it’s set up almost the exact same as in the test scene.
Thanks for any light you’re able to shed on this one :).
Ok I’ve got an answer, finally! The problem was because of all non-sprite components defaulting to be in the “default” sorting layer. I though it wasn’t possible to edit the sorting layer or layer order of non-sprite elements since there are no sorting options in their renderers, but apparently those options are accessible via code and are just not available in the Inspector.
More info here
http://forum.unity3d.com/threads/211822-Using-non-Sprites-with-the-new-sorting-layers
Anyway, by using
GetComponent().sortingLayerID = blah; and
.sortingOrder = blah;
I was able to set the rendering depth of the text. Thanks for the advice though Young :).
Alright, I had this problem too and the only solutions i could find were SUPER complicated. Here, just attach this simple script to any game object with a mesh renderer.(C#)
using UnityEngine;
using System.Collections;
public class RendererSorter : MonoBehaviour {
void Start () {
this.gameObject.GetComponent<MeshRenderer>().sortingLayerName = "Foreground";
//or whatever your layer name is
this.gameObject.GetComponent<MeshRenderer>().sortingOrder = 50;
//or whatever order you want it in the layer
}
}
It is a useful pattern to write code that only runs in the editor, Unity internally serializes many fields such as the sorting layer and order, which we can take advantage of by creating editor only scripts. This is especially important on mobile platforms, where performance is critical.
[ExecuteInEditMode]
[RequireComponent(typeof(TextMesh))]
public class CONTENT_Text : MonoBehaviour
{
public string sortingLayerName = "Default";
public int orderInLayer = 0;
#if UNITY_EDITOR
public void Update ()
{
gameObject.GetComponent<MeshRenderer>().sortingLayerName = sortingLayerName;
gameObject.GetComponent<MeshRenderer>().sortingOrder = orderInLayer;
}
#endif
}
When you render anything gui related (text, rect, etc), you can use
GUI.depth to set render priority, for example GUI.depth=0; is on top of everything.
setting my default layer to render on top seems to have fixed my issues
You can actually achieve this without any scripts. Additionally, you will can see changes in scene without running the game.
-
Go to: Edit -> Project Settings -> Editor.
-
Asset Serialization: Force Text
-
Create some Sorting Layer.
-
Create some temporary object with Sprite Renderer and set Sorting Layer to created one.
-
Switch to debug mode.
-
Select and copy Local Identifier In File of Sprite Renderer (Ctrl + C).
-
Open your scene file (e.g. main.unity) with any text editor (Notepad, Notepad++…)
-
Search (usually Ctrl + F) for this ID in file. You have to find something like this:
— !u!212 &713548566
SpriteRenderer:
-
Search for similar string below and copy it (Ctrl + C):
m_SortingLayerID: 1913257769
-
Search for your object(s) with TextMesh in similar way.
-
Paste this string on the new string after MeshRenderer: and save the file.
-
Return to Unity, it will ask to reload scene, accept it. You can now delete temporary object. Also, if you have done everything correctly, you should see changes right in the scene without running the game.
You can set Sorting Order in the similar way.