While switching a few things to TextMesh Pro after UT obtained it (great job on that by the way, much love) I realised that formatting, in the sense of rich text, don’t format themselves automatically.
Using the ascii table key \u2022 as a bullet point, and <indent=1em> for padding, my text doesn’t format by itself.
But once I edit the text in the Unity Editor (remove and re-add a space for example), it displays correctly.
public TextMeshProUGUI displayPage;
void Start()
{
displayPage.text = displayText;
}
Does anyone know of a way to fix this, am I missing an option or something?
Based on how the text appears over each other, check to make sure that if you copy / pasted the text that you don’t have some hidden carriage return characters in that string / label.
The text that is being assigned to it is being done by passing the text from a text file (TextAsset) into the display.
I’m not sure whether anything like a return might be added somehow while doing that, but in the file at least, there is no hidden returns or anything.
This is happening for me too - the \u2022 gets displayed as \u2022 rather than as a bullet point unless I edit something in the inspector. The text comes from an XML file. If I copy and paste the same text into something like
text.text = "\u2022 A bullet point";
It works fine. It’s only when the text is coming from an external file that there’s a problem.
Logging the text to the console shows that the text is \u2022, as it should be - there’s no double escape happening. I double checked using string.replace to change \ to , but nothing changed.
If the issue only happens when the string comes from an external file then it has to be related to double escaping. Although that is what I believe you did, re-check the unicode value of each character from that file by creating a for loop and iterating over stringText to see the value. This should shed some light on the behavior.
A simple scene would be great but perhaps look at the source text file might reveal the issue and spare you from having to create the scene. You can email me or PM me a link to the source text file.
Yep I’m certainly seeing it here in Unity 2018.2 using TextMesh Pro. You can’t simply tell the component to change the text from say \uf028 to \uf026 (I’m using Font Awesome) I have to do the whole Convert.ToChar(0xf026).ToString(). Not a deal breaker but seems strange that this still doesn’t seem to work.
Here is my example:
public string toggleUnicode = "f026";
public string defaultUnicode = "f028";
private bool _altToggleStateShowing;
int code = int.Parse(_altToggleStateShowing?toggleUnicode:defaultUnicode, System.Globalization.NumberStyles.HexNumber);
btnIconText.text = System.Convert.ToChar(code).ToString();
The issue here isn’t related to TextMesh Pro but to how the Unity Editor text field handles escape characters.
using UnityEngine;
using TMPro;
public class Sandbox_01 : MonoBehaviour
{
public string someText = "\uf026";
public TMP_Text TextComponent;
private void Awake()
{
// This will work fine.
TextComponent.text = "\uf026";
// This will work fine as well.
TextComponent.SetText("\uf026");
// This is tricky since the Unity Editor text field will escape the "\" and instead of converting the value into a single unicode
// This will result in a string that contains '\' + 'u' + 'f' + '0' + '2' + '6' or 6 individual characters.
TextComponent.text = someText;
}
}
If you test the above code, one text line at a time of course, you will see that issue with the public field. If you convert that to a private field, then the text will display correctly again.
In my opinion, this special handling of escape characters in a public field in the inspector is very confusing. When I have time, this is something that I would like to revise.
P.S. Unless you are trying to combine text with some value, I would suggest you use the .text property instead of SetText().
OK cool good to know thanks. However whole point is to have it exposed for editing in the Editor. Oh well yet another Unity idiosyncrasy to mark down and remember.
That is exactly why I want to revise how Escape characters are handled in Editor text fields as most users who run into this are looking to do something similar to you.
I don’t know if it has been fixed in Unity 2019.x but encountered the same issue in 2018.3.1f. I am reading a text from a file and assigning it to a TextMeshPro component in the scene.
After reading the comments here, I solved the problem in the following way:
It’s 2020, the year of the corona, and this is still an issue with text entered into the editor and/or stored in serializedObjects. While the workaround of replacing solves the issue, it’s not terribly great when you have to manually implement it for a multitude of characters outside the standard few.
@Stephan_B Any news on the character escaping update? It’s a tad messy/cumbersome to daisy chain several .Replace()'s for each supported non-standard character that a user can enter into the custom editor. (more than just basic bullet-discs)
The escaping of control characters is a Unity Editor behavior that has been there for a long time. I can speak to the Editor team again to see if we can get that behavior changed.
Stumbled upon that annoyance and got it fixed by using Regex.Unescape(myText) using System.Text.RegularExpressions. The text in the editor is in the following format: \uE901