< Back to Game Dev, Unity

Using non-UI text in Unity

An example of non-UI text is text on an in game playing card.

To do this, use the ‘Text Mesh’ Component. For the font, you want to set it’s font size to something like 100 in the Resources folder, otherwise the font will be blurry. Lastly, the text mesh uses the mesh renderer to render the text, which does not have sorting layers by default. This makes it hard to position it’s layer compared to other sprites which do use sorting layers. To add sorting layers to text mesh, make a new script file called ‘MeshRendererSortingLayersEditor’ and put it in a folder called ‘Editor’ in the ‘Assets’ folder. The code is below and copied from http://2dtoolkit.com/forum/index.php?topic=3572.0. Then restart Unity and sorting layers will appear in the mesh renderer component.

The code:

using System;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.Reflection;

public class MeshRendererSortingLayersEditor : Editor
    Renderer renderer;
    string[] sortingLayerNames;
    int selectedOption;
    void OnEnable()
        sortingLayerNames = GetSortingLayerNames();
        renderer = (target as Renderer).gameObject.GetComponent<Renderer>();
        for (int i = 0i<sortingLayerNames.Length;i++)
            if (sortingLayerNames[i] == renderer.sortingLayerName)
                selectedOption = i;
    public override void OnInspectorGUI()
        if (!rendererreturn;
        selectedOption = EditorGUILayout.Popup("Sorting Layer"selectedOptionsortingLayerNames);
        if (sortingLayerNames[selectedOption] != renderer.sortingLayerName)
            Undo.RecordObject(renderer"Sorting Layer");
            renderer.sortingLayerName = sortingLayerNames[selectedOption];
        EditorGUILayout.LabelField("(Id:" + renderer.sortingLayerID.ToString() + ")"GUILayout.MaxWidth(40));
        int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer"renderer.sortingOrder);
        if (newSortingLayerOrder != renderer.sortingOrder)
            Undo.RecordObject(renderer"Edit Sorting Order");
            renderer.sortingOrder = newSortingLayerOrder;
    // Get the sorting layer names
    public string[] GetSortingLayerNames()
        Type internalEditorUtilityType = typeof(InternalEditorUtility);
        PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames"BindingFlags.Static | BindingFlags.NonPublic);
        return (string[])sortingLayersProperty.GetValue(nullnew object[0]);

Leave a Reply

Your email address will not be published. Required fields are marked *