How to Use Emoji's In unity

714 views Asked by At

I want to use the Emoji's in my project. but as we all know that unity is not supporting the all emoji's. First I try to make the Sprite asset and with it I can use many emoji but the Emoji's with the complex Unicode like "1f3c3_1f3fb_200d_2642" this Emoji.

I want to use all the Emoji's in unity Using the TextMeshPro. Is there any way to make it happen in unity. Thanks in Advance.

1

There are 1 answers

13
shingo On

You should prepare your sprite asset first, here is an example that uses the EmojiIterator class to convert long emoji sequences to TMP's sprite tags.

static string ProcessText(string text)
{
    var itor = new EmojiIterator(text);
    var sb = new StringBuilder();
    while(itor.MoveNext())
    {
        if(itor.SequenceLength <= 2) //TMP can handle single emoji characters.
            sb.Append(itor.Sequence);
        else //use sprite asset
            sb.Append($@"<sprite name=""{ConvertToName(itor.Sequence)}"">"
    }
    return sb.ToString();
}

The ConvertToName method is used to convert a string like "\U0001f3c3\U0001f3fb\u200d\u2642" to "1f3c3_1f3fb_200d_2642".

static string ConvertToName(string text)
{
    var sb = new StringBuilder();
    for (int i = 0; i < text.Length; i++)
    {
        var j = char.ConvertToUtf32(text, i);
        if (j > 0xffff)
            i++;
        sb.Append(j.ToString("x"));
        sb.Append('_');
    }
    return sb.Length > 0 ? sb.ToString(0, sb.Length - 1) : "";
}