TMPpro and unicode characters

So im designing an enemy ai robot that relays its mood or intention to the player by a display - it’s “hooman interaction interface” . The display shows an emoji of the robots current intention. Since I’m using a GOAP style ai for this enemy (if you want to know more about GOAP style ai, you can read my post on it here ) I thought it would be practical to have the base_action present the relevant emoji on the display. This way the display is updated every time the action is changed. First of, I need a font that has the suitable emoji characters. I found NotoEmoji, which is suitable because of its license, as well as it contains all the relevant symbols. Installing that in Unity is done by importing the font, right-clicking it and select TMP->Create Font Asset. Depending on how many characters you need, you may want to select the relevant character by its ASCII reference. When done, you have a usable font for the TMP_Text field. This is all well, but also as far as smooth sailing goes.

Parsing unicode

For presenting emoji, or any character by unicode reference, comes with a catch-22-like problem. Referencing a unicode in by entering the text in TMP_Text text box can be done by writing the unicode including the escape character ie. \U0001F610 (for the indifferent smiley emoji). This is fine when assigning the text through Unity Editor, as long as you tell TMP_Text to parse escape characters. The problem arises when trying to set the text through scripting. Each Action in my GOAP ai inherits from the Action_Base class. I simply allocate a string for that class, since TMP_Text.text requires a string for any text to show. You cannot simply write a string in C# like “\U0001F610” , this will not parse, you have to tell it not to parse the escape character by either “\U0001F610” or @”\U0001F610” . This means the string special character “" is not parsed. Since we want it to parse, this is not good. Now TMP_Text just shows the text as “U0001F610”.

StyleSheet

Solution

I am aware that the problem is a lot more complex if you need localization for other written language characters. But my problem was limited to the fact that I wanted to print a handful of emoji as text. I decided to create a stylesheet and reference it in the TMP_Text. Each style element would be a key and contain the relevant unicode.

StyleSheet

Furthermore the key can be anything really, thus for practicality can be the same as the Actions name. This way we do not need to manually allocate a string on each Action but rather just lookup the Action.Name when updating the text through the relevant style tag.

TMP_Text hoomanTextInterface;
hoomanTextInterface.text = "<style=" + bestAction.GetType().Name + "></style>";

Closing

I considered alternate ways of printing the emoji, the most obvious one was to create a sprite atlas and reference that through a dictionary and perhaps ditch the TMP_Text and use an Image element instead. However since I perhaps want the display on the robot present other text, I stuck with my initial concept. This took what I feel was too long for it being such a miniscule detail, but now that it works, its pretty solid as long as the client can read the font. I hope this post provides a little assistance to some of you who want to use emoji and TextMeshPro in a game. Thank you for reading.

SmilingRobatrice