Exemplo n.º 1
0
        private IEnumerator WritingText()
        {
            // 富文本变量
            Dictionary <int, int> richLengthDict;
            Dictionary <int, KeyValuePair <string, string> > richTextDict;
            int richCount = RegexUtility.GetRichTextFormatString(m_Text, out richLengthDict, out richTextDict);

            int    index         = 0;            // 正在写入的下标
            int    richIndex     = 0;            // 正在写入的富文本下标
            string curText       = string.Empty; // 当前Text组件文本
            string richTextInset = string.Empty; // 富文本内的普通文本
            string richText      = string.Empty; // 富文本

            while (txtText.text != m_Text)
            {
                if (wordInterval <= 0f)
                {
                    yield return(null);
                }
                else
                {
                    yield return(new WaitForSeconds(wordInterval));
                }

                // 如果存在富文本
                if (richCount > 0 && richLengthDict.ContainsKey(index))
                {
                    // 如果没有内容:例如 "<color=red></color>"
                    if (string.IsNullOrEmpty(richTextDict[index].Value))
                    {
                        richText = richTextDict[index].Key;
                    }
                    else
                    {
                        richTextInset += richTextDict[index].Value[richIndex++];
                        richText       = string.Format(richTextDict[index].Key, richTextInset);
                        txtText.text   = curText + richText;
                    }

                    // 富文本写入完成
                    if (richText.Length == richLengthDict[index])
                    {
                        curText += richText;
                        richCount--;
                        richIndex     = 0;
                        richTextInset = string.Empty;
                        richText      = string.Empty;
                        index        += richLengthDict[index];
                    }
                }
                else
                {
                    curText     += m_Text[index++];
                    txtText.text = curText;
                }
            }

            OnTextWriteDone();
            m_WritingCoroutine = null;
        }