Text mesh (font material) renders on top of everything, any way to change that?

I have these objects with some text on them floating in my space (labeling things as it were). My problem is that apparently, text is drawn last, so it is visible always, even if it should be occluded by intervening solid objects. Is there a way to fix that? Do I have to muck with the shader on it?

Yes, you have to use a different shader. Here's one to start with:

http://www.unifycommunity.com/wiki/index.php?title=3DText

The shader has been moved here: http://wiki.unity3d.com/index.php?title=3DText

	Shader "GUI/3D Text Shader" { 
	Properties { 
	   _MainTex ("Font Texture", 2D) = "white" {} 
	   _Color ("Text Color", Color) = (1,1,1,1) 
	} 
	
	SubShader { 
	   Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } 
	   Lighting Off Cull Off ZWrite Off Fog { Mode Off } 
	   Blend SrcAlpha OneMinusSrcAlpha 
	   Pass { 
		  Color [_Color] 
		  SetTexture [_MainTex] { 
			 combine primary, texture * primary 
		  } 
	   } 
	} 
}

This one won’t use the color set with TextMesh though.

I just got this working in my most recent project. That link someone else posted has instructions how to use it and you can set the colour too! (Link: http://wiki.unity3d.com/index.php?title=3DText)

Code

Shader "GUI/3D Text Shader" { 
	Properties {
		_MainTex ("Font Texture", 2D) = "white" {}
		_Color ("Text Color", Color) = (1,1,1,1)
	}
 
	SubShader {
		Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
		Lighting Off Cull Off ZWrite Off Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha
		Pass {
			Color [_Color]
			SetTexture [_MainTex] {
				combine primary, texture * primary
			}
		}
	}
}

This is very old and may not be relevant any more, but in case someone still has this issue I just found out a super-easy solution to this problem: just set everything else’s material’s Rendering Mode to “transparent”. In that way the right order of drawing is preserved.

@Ngoc Ngo
Any way to get the color set with TextMesh working?

As some people might still want an answer to this, the TextMesh Color seems to set the Vertex colors of the generated mesh, so the “_Color” field in the shader is useless and you should instead add the Vertex Color in the shader.

Simple solution - use the builtin “Font.shader” and remove the the “ZTest Always” and the useless “_Color” parts:

Shader "GUI/3D Text Shader"
{
	Properties
	{
		_MainTex("Font Texture", 2D) = "white" {}
	}

	SubShader
	{
		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"PreviewType" = "Plane"
		}
		Lighting Off
		Cull Off
		ZWrite Off
		Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
			#include "UnityCG.cginc"

			struct appdata_t {
				float4 vertex : POSITION;
				fixed4 color : COLOR;
				float2 texcoord : TEXCOORD0;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};

			struct v2f {
				float4 vertex : SV_POSITION;
				fixed4 color : COLOR;
				float2 texcoord : TEXCOORD0;
				UNITY_VERTEX_OUTPUT_STEREO
			};

			sampler2D _MainTex;
			uniform float4 _MainTex_ST;

			v2f vert(appdata_t v)
			{
				v2f o;
				UNITY_SETUP_INSTANCE_ID(v);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.color = v.color;
				o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
				return o;
			}

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 col = i.color;
				col.a *= tex2D(_MainTex, i.texcoord).a;
				return col;
			}
			ENDCG
		}
	}
}

Edit:
alternatively without the CGPROGRAM

Shader "GUI/3D Text Shader"
{
	Properties
	{
		_MainTex("Font Texture", 2D) = "white" {}
	}

	SubShader
	{
		Tags
		{ 
			"Queue" = "Transparent" 
			"IgnoreProjector" = "True" 
			"RenderType" = "Transparent" 
		}
		Lighting Off
		Cull Off 
		ZWrite Off 
		Fog{ Mode Off }

		Blend SrcAlpha OneMinusSrcAlpha
		Pass
		{
			ColorMaterial AmbientAndDiffuse
			SetTexture[_MainTex]
			{
				combine primary, texture * primary
			}
		}
	}
}

Shader “GUI/3D Text Shader - Cull Back” {
Properties{
_MainTex(“Font Texture”, 2D) = “white” {}
_Color(“Text Color”, Color) = (1,1,1,1)
}

        SubShader{
        Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
        Lighting Off Cull Back ZWrite Off Fog{ Mode Off }

        Blend SrcAlpha OneMinusSrcAlpha
        Pass{
            Color[_Color]
            ColorMaterial AmbientAndDiffuse
            SetTexture[_MainTex]{
                combine primary, texture * primary
            }
        }
    }
}