Example #1
0
    private IEnumerator mSetUITextThatHasIconByAtlas(Text textToEdit, string inputString, Texture atlas, int normalAtlasType = 0)
    {
        var dataMap = normalIcon[normalAtlasType];

        if (dataMap == null)
        {
            Debug.LogError("没有填充Type为:" + normalAtlasType + "的Map");
            yield return(0);
        }
        Regex regex            = new Regex(regexTag);
        var   replaceEmojiList = new List <EmojiItem>();

        EmojiEffect emojiEffect = textToEdit.GetComponent <EmojiEffect>();

        if (emojiEffect == null)
        {
            emojiEffect = textToEdit.gameObject.AddComponent <EmojiEffect>();
        }
        int length = 0;
        int count  = 0;

        foreach (Match match in regex.Matches(inputString))
        {
            var tagName = match.Value.ToString();
            var index   = match.Index - length + count;
            length += tagName.Length;
            count++;
            replaceEmojiList.Add(new EmojiItem(index, dataMap[tagName]));
        }
        var res = regex.Replace(inputString, emSpace.ToString());

        textToEdit.text = res;

        yield return(null);

        emojiEffect.SetEmojiByEmojiRawImage(atlas, replaceEmojiList);
    }
Example #2
0
    private IEnumerator mSetUITextThatHasEmojiByAtlas(Text textToEdit, string inputString, Texture emojiAtlas)
    {
        List <EmojiItem> emojiReplacements = new List <EmojiItem>();
        StringBuilder    sb          = new StringBuilder();
        EmojiEffect      emojiEffect = textToEdit.GetComponent <EmojiEffect>();

        if (emojiEffect == null)
        {
            emojiEffect = textToEdit.gameObject.AddComponent <EmojiEffect>();
        }

        int i = 0;

        while (i < inputString.Length)
        {
            string singleChar = inputString.Substring(i, 1);
            string doubleChar = "";
            string fourChar   = "";

            if (i < (inputString.Length - 1))
            {
                doubleChar = inputString.Substring(i, 2);
            }

            if (i < (inputString.Length - 3))
            {
                fourChar = inputString.Substring(i, 4);
            }

            if (this.emojiRects.ContainsKey(fourChar))
            {
                // Check 64 bit emojis first
                sb.Append(emSpace);
                emojiReplacements.Add(new EmojiItem(sb.Length, emojiRects[fourChar]));
                i += 4;
            }
            else if (this.emojiRects.ContainsKey(doubleChar))
            {
                // Then check 32 bit emojis
                sb.Append(emSpace);
                emojiReplacements.Add(new EmojiItem(sb.Length, emojiRects[doubleChar]));
                i += 2;
            }
            else if (this.emojiRects.ContainsKey(singleChar))
            {
                // Finally check 16 bit emojis
                sb.Append(emSpace);
                emojiReplacements.Add(new EmojiItem(sb.Length, emojiRects[singleChar]));
                i++;
            }
            else
            {
                sb.Append(inputString[i]);
                i++;
            }
        }

        // 设置文字
        textToEdit.text = sb.ToString();

        yield return(null);

        emojiEffect.SetEmojiByEmojiRawImage(emojiAtlas, emojiReplacements);
    }