Using non-UI text in Unity

Posted on

in

,

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;

[CanEditMultipleObjects()]
[CustomEditor(typeof(MeshRenderer))]
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()
    {
        DrawDefaultInspector();
        
        if (!rendererreturn;
        
        EditorGUILayout.BeginHorizontal();
        selectedOption = EditorGUILayout.Popup("Sorting Layer"selectedOptionsortingLayerNames);
        if (sortingLayerNames[selectedOption] != renderer.sortingLayerName)
        {
            Undo.RecordObject(renderer"Sorting Layer");
            renderer.sortingLayerName = sortingLayerNames[selectedOption];
            EditorUtility.SetDirty(renderer);
        }
        EditorGUILayout.LabelField("(Id:" + renderer.sortingLayerID.ToString() + ")"GUILayout.MaxWidth(40));
        EditorGUILayout.EndHorizontal();
        
        int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer"renderer.sortingOrder);
        if (newSortingLayerOrder != renderer.sortingOrder)
        {
            Undo.RecordObject(renderer"Edit Sorting Order");
            renderer.sortingOrder = newSortingLayerOrder;
            EditorUtility.SetDirty(renderer);
        }
    }
    
    // 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 *

About me

Mark Wong is a front end developer with 10+ years experience. Most of his knowledge of HTML5, CSS and Js is self taught.

Calendar

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930