Unity is a popular game development engine that provides many tools to create interactive and engaging games. One of the most important aspects of any game is the user interface, which includes text elements that provide information and feedback to the player. Unity provides two options for creating text elements: the legacy Text component and the newer TextMeshPro.
- The Text components in Unity
- TextMeshPro v legacy Text
- How to switch to TextMeshPro
- How to use TextMeshPro
- Conclusion
1. The Text components in Unity
Text is the legacy component for displaying text in Unity since its inception. It is a simple and straightforward system that renders text using a bitmap font. Legacy Text has been available since Unity 4.6 and is still used in some older projects. On the Other hand, TextMeshPro is a newer and more advanced text rendering system that was introduced in Unity 2018.3. It is a powerful and flexible system that overcomes many of the limitations of legacy Text.
Starting from Unity 2020.1, the legacy Text component is no longer available in the default installation. If you need to use it, you can still import it from the Unity Package Manager. However, it is recommended to switch to TextMeshPro which is available in the default installation of Unity since version 2018.3 for better performance and more advanced features.
2. TextMeshPro v legacy Text
TextMeshPro provides several advantages over the legacy Text component, making it the better choice for modern game development. Some of the key advantages include:
- Rich text formatting: TextMeshPro supports rich text formatting, which allows developers to change the color, font size, style, and other properties of individual words or characters. This makes it easier to create visually appealing text that stands out.
- Scalability: TextMeshPro uses vector graphics to render text, which means that it can scale infinitely without loss of quality. This makes it ideal for high-resolution displays and different screen sizes.
- Performance: TextMeshPro is highly optimized and can render large amounts of text quickly and efficiently. This makes it ideal for games that require a lot of text, such as RPGs and visual novels.
- Advanced features: TextMeshPro includes advanced features like sub-pixel positioning and complex text layout and formatting, which allow developers to create complex text effects.
We are also putting an article together detailing more about the differences between TextMeshPro and legacy Text. Stay tuned!
3. How to switch to TextMeshPro
If you are using the legacy Text component and want to switch to TextMeshPro, you may need to modify your code to account for the differences between the two systems. For example, TextMeshPro uses a different API for accessing text properties, and it has some additional features that may require changes to your code. To use TextMeshPro in Unity, you need to add the TextMeshPro package to your project. You can do this by following these steps:
- Open the Unity Package Manager by selecting “Window” from the main menu and then choosing “Package Manager.”
- In the Package Manager, click on “Packages: Unity Registry” and search for “TextMeshPro.”
- Click on the “TextMeshPro” package to select it, and then click the “Install” button.
4. How to use TextMeshPro
Once the package is installed, you can add a TextMeshPro component to your game object by right-clicking the game object and selecting “UI” in the dropdown menu, then selecting “Text – TextMeshPro“.
If you have existing code that uses the Text component, you will need to update the code to use the TextMeshPro component instead.
You also need to change the text type variable from Text to TextMeshProUGUI to use the TextMeshPro component instead of the Text component.
With these changes, the code should work with the TextMeshPro component. You can also take advantage of the advanced features of TextMeshPro, such as rich text formatting and text alignment options, by using the appropriate properties and methods of the TextMeshPro component.
It’s also a good idea to remove the legacy text component from your GameObject to avoid any potential conflicts or performance issues.
5. Conclusion
In conclusion, TextMeshPro is a more advanced and powerful text rendering system that provides many benefits over the legacy Text component. If you are using an older version of Unity and cannot use TextMeshPro, you can still use the Text component, but it is recommended to switch to TextMeshPro if possible. If you do decide to switch, you may need to make some changes to your code to account for the differences between the two systems.