'TextMeshPro not properly displaying "\n" new lines
In my game, I am grabbing a random string from a text file and Changing a GameObjects TextMeshPro.Text to this line.
[SerializeField] private TextMeshProUGUI endOfContentText;
[SerializeField] private TextAsset messageTextFile;
private void OnEnable() {
TextChangerOuter(ParseTextAsset());
}
private void ParseTextAsset() {
var arrayString = messageTextFile.text.Split('\n');
foreach (var line in arrayString) {
listOfWords.Add(line);
}
}
private void TextChangerOuter(string text) {
var rand = new Random();
var randomKey = listOfWords.ElementAt(rand.Next(0, listOfWords.Count));
listOfWords.Remove(randomKey);
endOfContentText.text = randomKey;
}
The problem I am experiencing is that some line in this file contains "\n" new lines as part of the joke. At first, I was worried that the ParseTextAsset() method would split this line up creating many blank lines but that did not happen. What I did not expect was for TextMeshPro to not parce the following line correctly.
Oddly if I go into the inspector and change the text manually, or turn the TextMeshPro component off and back on manually the text reformats as expected.
In the past I have used Canvas.ForceUpdateCanvases(); to fix some aspects of my UI, but this time its not working this time. I even wrote a Coroutine to disable and re-enable this component after it generates the text but that did not work.
private IEnumerator SetShowNewMessageAfterFrame() {
yield return new WaitForEndOfFrame();
endOfContentText.enabled = false;
yield return new WaitForEndOfFrame();
endOfContentText.enabled = true;
Canvas.ForceUpdateCanvases();
}
EDIT:
As per Berk Askin's answer Project Settings / TextMeshPro / Settings > [Parse Escape Sequence] was already set and produced the above results.
Solution 1:[1]
TMpro components has option to show/hide escape sequences, you just need to turn it on at Project Settings / TextMeshPro / Settings and find [Parse Escape Sequence] option and open it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Berk Askin |


