Text Mesh Pro

To those of you who are screaming at the screen telling Ben to use TextMeshPro (I did), here is what the few changes are to make it happen:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using TMPro; //IMPORTANT Change #1

public class ScoreBoard : MonoBehaviour {

[SerializeField] int scorePerHit = 12;
int score;
TextMeshPro textMeshScore; // Change #2

// Use this for initialization
void Start () {
	textMeshScore = GetComponent<TextMeshPro>(); // Change #3
	textMeshScore.text = "0";
}

public void ScoreHit()
{
	score += scorePerHit;
	textMeshScore.text = score.ToString();
}
}

The rest is the same.

And for all the idiots, like me, it doesn’t work as on screen ui…: Change the Canvas render mode to “Screen-Space - Camera”, set the camera as main camera and set the plane distance to something small like 1.

For what it’s worth, I’m using TextMeshPro with my Canvas set to Screen Space - Overlay. So far the only trouble I can think of is when I had a mis-placed anchor, so scaling was giving me unexpected results; as soon as I corrected the anchor, no more troubles.

I’ve gone back to re-do the Boost project, and have been using it there to see an example. I honestly don’t even remember why I decided to use it over the built-in option(s).

2 Likes

To make it work for UI you have to use another Type.
It’s TextMeshProUGUI instead of TextMeshPro :wink: !

Here is my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class ScoreBoard : MonoBehaviour
{
    [SerializeField] int scorePerHit = 19;
    
    int score;
    TextMeshProUGUI scoreText;


    // Start is called before the first frame update
    void Start()
    {
        scoreText = GetComponent<TextMeshProUGUI>();
        scoreText.text = score.ToString();
    }

    public void ScoreHit()
    {
        score = score + scorePerHit;
        scoreText.text = score.ToString();
    }
}
1 Like

What is the advantage of TextMeshPro over the basic Text object?

Oh there are many:

  1. It always scales the text perfectly so there is no pixelation (it does it automatically all the time)
  2. You can instantly choose the font type (Bold, Italic, Underline, Strikethrough)
  3. Way more alignment options (Justified, Geometry centre, etc)
  4. Colour gradients
  5. Outline and underlay settings in the same shader
  6. It’s free and can probably do more than I know of

The main reason I use it is because of number 1

Gotcha. That is a good list of advantages. Thank you!

1 Like

You’re welcome :>

Privacy & Terms