Baraphor
Baraphor

Reputation: 259

Adding emojis properly to a game

We are trying to add emojis our game using Emoji One and Unity with TextMeshPro. We have a window opening up which shows all the emojis and then the user can pick the one they want, we have run into problems with deleting them and typing text after them.

Here is the code we have have, the name of the sprite is this: 1f609, however the output when we run the code and add it to the input field is this: \ud83d\ude09

string name = _image.sprite.name;
int hexNumber = int.Parse(name, System.Globalization.NumberStyles.HexNumber);
string c = char.ConvertFromUtf32(hexNumber);
_inputField.text += c;

The expected result is that we should see just a single unicode character (I think) so that we can properly delete emoji's with the backspace key and when you enter an emoji it does not create two characters to be deleted and it does not place the character between the two emoji spots so when we enter text it breaks.

Right now if we delete text it will leave us with empty square and if we add text afterwards through typing in the box it will split the characters in two breaking them.

Upvotes: 2

Views: 1317

Answers (1)

Dialecticus
Dialecticus

Reputation: 16789

Unicode in C# world means UTF-16, so there are these "surrogate pairs" that you have to deal with now. One solution would be to detect if you just removed one half of a surrogate pair and in that case remove the other half as well. First half of a surrogate pair is always in range [D800h, DBFFh] and second half is always in range [DC00h, DFFFh], so when you remove a char in one of those ranges you know that you should remove one more char, and you also know where that char is (before or after the one just removed).

Upvotes: 1

Related Questions