Hello everyone. I’m introducing localization into my game, and I;m running into trouble when I try to programatically change the font of a Text Mesh Pro asset when a button is pressed. This is the script:
The font or text doesn’t update until the Text Mesh Pro’s object is disabled and re-enabled. I’m not entirely sure if I’m doing this correctly. Could someone pleas offer some assistance?
Unless you want to use a different material preset than the default material of the font asset, you don’t need to assign a new material. Assigning a new font asset will take care of also changing the material preset.
There could be something else going on with your script / implementation that is affecting this.
Here is a simple script which will switch font asset and in this case use a different material preset for both fonts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SandBox_32 : MonoBehaviour
{
public TMP_FontAsset FontAssetA;
public TMP_FontAsset FontAssetB;
public Material FontMaterialA;
public Material FontMaterialB;
private TMP_Text m_TextComponent;
private void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
IEnumerator Start()
{
// Assign the new font asset.
m_TextComponent.font = FontAssetA;
// Use a different material preset which was derived from this font asset and created using the Create Material Preset Context Menu.
m_TextComponent.fontSharedMaterial = FontMaterialA;
yield return new WaitForSeconds(2.0f);
m_TextComponent.font = FontAssetB;
m_TextComponent.fontSharedMaterial = FontMaterialB;
}
}
Hi, @Stephan_B , your help will be appreciated. In my case, I want next with world space text.
In runtime, I want to customize (with help of GUI) font main settings (font size, color…) and font material settings (outline color & outline thickness) - no problem with it.
Next, I want to save the changes/state of the whole text somehow.
Material settings can be saved by creating material from the current font material & assigning it to the fontSharedMaterial. But this doesn’t work. Why? I don’t want to change the font asset itself.
Font Main Settings can be saved from serialization/derealization, I guess, but I didn’t make an investigation in it. So, any tips will be helpful, if you know other ways or examples.
Apply the changes/state to other text game objects in world space that uses the same font/material.
I’ve found an issue when changing fonts programmatically while also trying to change the outline color directly afterwards.
The font will change, however, setting the outline color, will set the outline of the previous font’s material. This causes the Text Mesh Pro component to switch back to the old font’s material and render incorrectly, like so:
You can fix this in the inspector by altering the text or even reselecting the current Material in the Material Preset drop-down list. But this leaves you with the correct font, and the default (incorrect) outline color. I have not been able to find a solution that will work at run-time unfortunately, which means I can’t fix the issue for my game
I tried delaying the point at which I would set the outline color of the text. This does not seem to resolve the issue as the reference to the outline color is still using the old font’s material if I grab it in the same method (after setting the new font).
It’s a little tricky to send an example but I’ll work on creating a test project later today:
Found here: shorturl.at/atDL2
The forum won’t let me hyperlink due to spam blocking
August 6, 2023 – Update, Fix, and Potential Bug Discovered
An update on a bug I found while working through this issue. In my case, I’m swapping two very different fonts for the same characters (an English and an Aurebesh (Star Wars) font). To do this, I have prefabs that have the correct TextMeshPro settings for each place that I use the different fonts (I have different settings for how the font should look on a button vs. another part of the UI). I use these prefabs as an example and then copy the important settings from them to the “target” TMP_Text Component. Here is my code to do so:
Note that I’m using several tricks mentioned above, including enabling and disabling the TMP_Text component, setting text to “” and then setting it back, and setting the gameObject to be inactive and then active. I have not experimented to see which (if any) of these are actually required.
The new thing that I did find is that TextMeshPro will not correctly copy over Material Preset settings from the automatically-created version of the Material Preset. When using example TMP_Text fields that used the default Material Preset, outlines were broken, and other aspects of the fontSharedMaterial just didn’t work. On the other hand, if I manually create a Material Preset (see image below) and assign it to the example TMP_Text Component, then the fontSharedMaterial settings do copy over to the target TMP_Text instance correctly.