How to draw text over sprite

How can I draw text label over Sprite (new Unity 4.3) feature.

I have sprite hirarchy

Screen (base background sprite: order in layer = 0)
-> LoadingProgress (progress bar background sprite: order layer = 1)
 +--> Progress (progress bar bar sprite: order layer = 2)
 +--> Text (progress bar text)

I have progress bar script (on LoadingProgress GameObject) which automatically takes Progress and Text objects if they exist and updates Progress scale and Text text to current progress values (ex. 10/100)

The problem is Text is not visible.

Have tried to make Text as TextMesh - than it is only visible if progress is empty - Progress sprite is drawn on top of Text.

Have tried to make Text a GUIText - but have a problem converting world coordinates to GUI coordiantes. GUIUtility.ScreenToGUIPoint(Camera.main.WorldToScreenPoint(text.transform.position)) always returns (0, 0) so text is drawn in lower left corner of the screen.

upd: Figure out that GUIUtility.ScreenToGUIPoint is only working in OnGUI() function.

Currently, all non-sprites seem to go into the “Default” sorting layer. So if you put any sprites in a Sorting Layer or Order in Layer that is in front of Default/0, no amount of Z offsetting is going to get the non-sprite in front of the sprite. For now, I’m putting my UI Sprites back in my “Default” sorting layer and using position.z and the Offset Z field in TextMesh to push it in front of my sprite.

Another option I have found is to put the textmesh entity’s renderer to be the same layer as another entity’s.

I have put my textmesh as a child of an entity with a sprite renderer and set the sorting layer to be that of the parent’s.

void Start () 
{
    this.renderer.sortingLayerID =   
    this.transform.parent.renderer.sortingLayerID;
}

Another option is to place the “Default” default sorting layer in front of all other layers (drag/drop in editor).

Of course none of these answers are perfect, but just another option that might work for some. Would be nice if we could get a sorting layer for a TextMesh, that we can set in the editor.

Textmesh components are Renderer components as noted in a previous answer. Its easy to add your own component that sets the renderer SortingLayerID or SortingOrder to match the sprite you want the text to sit on top of (to be Z based). See sample.

public class RendererSortingLayer : MonoBehaviour {
	public int SortLayer = 0;
	public int SortingLayerID = SortingLayer.GetLayerValueFromName("Default");
	// Use this for initialization
	void Start () {
		Renderer renderer = this.gameObject.GetComponent<Renderer>();
		if(renderer != null)
		{
			renderer.sortingOrder = SortLayer;
			renderer.sortingLayerID = SortingLayerID;
		}
	}
	
}

Ok, I figured out that I should scale screen coordinates.

Vector2 screenPosition = Camera.main.WorldToScreenPoint(text.transform.position);
Vector2 guiPosition = new Vector2(screenPosition.x / Screen.width, screenPosition.y / Screen.height);
text.transform.position = GUIUtility.ScreenToGUIPoint(guiPosition);

Works ok. But one question still remains - Is it possible to draw TextMesh over sprite?

In Unity 5, Canvas does have Sorting Layer and Sorting Order. So if you have UI elements, such as UI text or buttons, that you want to mix with non-UI elements, you can simply put each layer of UI elements into its own canvas and apply the sorting settings that you want to that canvas.

Note: Within a canvas, sorting options are simply determined by order of the hierarchy.

Note 2: If a canvas is a child of another canvas, it will hide that option and inherit from the root canvas, but you can simply tick the “Override Sorting” option and set your own.

Alternatively, you can move default layer to top.
like this:
96244-1.png

For anyone currently looking for this answer. TextMeshPro will solve this. It has a field for order. It works flawlessly making the text a child of the sprite and setting the order to one level above.

It is currently free for use and has a small charge to it if you want to manipulate the source code. I’m using the free version.

Link is here: TextMesh Pro | Beta Projects | Unity Asset Store

Cheers!