Example #1
0
 private void LateUpdate()
 {
     if (updateSubs)
     {
         foreach (var vp in SpriteTagInfoDic)
         {
             var         index       = vp.Key;
             SpriteEmoji spriteEmoji = GetSpriteEmoji(index);
             spriteEmoji.SetAllDirty();
             updateSubs = false;
         }
     }
 }
Example #2
0
        private string GetOutputText(string inputText)
        {
            //回收各种对象
            ReleaseSpriteTageInfo();
            ReleaseHrefInfos();

            if (string.IsNullOrEmpty(inputText))
            {
                return("");
            }

            _textBuilder.Clear();
            int    textIndex = 0;
            int    newIndex  = 0;
            string part      = "";
            int    tempIndex = 0;
            int    vertSum   = 0;

            if (tagParser == null)
            {
                tagParser = new TagParser.TagParser();
            }
            tagParser.Reset();
            tagParser.ParseText(inputText);


            tagBegin = 0;
            _textBuilder.Append(inputText);
            for (int i = 0; i < tagParser.Tags.Count; i++)
            {
                var tag = tagParser.Tags[i];
                // _textBuilder.AppendFormat(g.TextWithNoTags.Substring(tagBegin, tag.startIndex - tagBegin));
                // tagBegin += tag.endIndex - tag.startIndex;
                switch (tag.Name)
                {
                case "sprite":
                {
                    int    spriteSub = 0;
                    string spriteTag = "";
                    for (int j = 0; j < tag.Properties.Count; j++)
                    {
                        var tempId  = tag.Properties[j].key;
                        var tempTag = tag.Properties[j].value;

                        if (tempId == "id")
                        {
                            spriteSub = int.Parse(tempTag);
                        }
                        if (tempId == "tag")
                        {
                            spriteTag = tempTag;
                        }
                    }
                    SpriteInfoGroup tempGroup = SpriteAssetManager.Instance.GetSpriteGroup(spriteSub, spriteTag);
                    if (tempGroup == null)
                    {
                        continue;
                    }
                    //清理标签
                    SpriteTagInfo tempSpriteTag = Pool <SpriteTagInfo> .Get();

                    tempSpriteTag.Index = (tag.endIndex - tag.lineIndex) * 4;

                    var s = fontSize / tempGroup.Size;
                    // _textBuilder.AppendFormat("<quad size={0} width={1}/>", tempGroup.Size * s, tempGroup.Width);

                    _textBuilder.Replace(string.Format("<{0}></sprite>", tag.content),
                                         string.Format("<quad size={0} width={1}/>", tempGroup.Size * s, tempGroup.Width));

                    tempSpriteTag.Id   = spriteSub;
                    tempSpriteTag.Tag  = spriteTag;
                    tempSpriteTag.Size = new Vector2(tempGroup.Size * tempGroup.Width, tempGroup.Size);
                    tempSpriteTag.UVs  = tempGroup.ListSpriteInfor[0].Uv;

                    //添加正则表达式的信息
                    SpriteTagInfoDic.TryGetValue(tempSpriteTag.Id, out var list);
                    if (list == null)
                    {
                        if (SpriteTagInfoDic.ContainsKey(tempSpriteTag.Id))
                        {
                            SpriteTagInfoDic.Remove(tempSpriteTag.Id);
                        }
                        list = new List <SpriteTagInfo>();
                        SpriteTagInfoDic.Add(tempSpriteTag.Id, list);
                    }
                    list.Add(tempSpriteTag);
                    SpriteEmoji spriteEmoji = GetSpriteEmoji(spriteSub);
                    spriteEmoji.m_spriteAsset        = SpriteAssetManager.Instance.GetSpriteAsset(spriteSub);
                    spriteEmoji.material.mainTexture = SpriteAssetManager.Instance.GetSpriteTexture(spriteSub);
                    spriteEmoji.ClearEmojiVert();
                    spriteEmoji.SetAllDirty();
                    break;
                }

                case "herf":
                {
                    int herfid = 0;

                    for (int j = 0; j < tag.Properties.Count; j++)
                    {
                        var tempId  = tag.Properties[j].key;
                        var tempTag = tag.Properties[j].value;
                        if (tempId == "id")
                        {
                            herfid = int.Parse(tempTag);
                        }
                    }
                    _textBuilder.Replace(string.Format("<{0}>{1}</herf>", tag.content, tag.Value),
                                         string.Format("<color=blue>{0}</color>", tag.Value));

                    var hrefInfo = Pool <HrefInfo> .Get();

                    hrefInfo.Id         = Mathf.Abs(herfid);
                    hrefInfo.StartIndex = (tag.startIndex - tag.lineIndex) * 4;; // 超链接里的文本起始顶点索引
                    hrefInfo.EndIndex   = (tag.endIndex - tag.lineIndex) * 4;;
#if UNITY_2019_1_OR_NEWER
                    hrefInfo.NewStartIndex = newStartIndex;
                    hrefInfo.NewEndIndex   = newIndex - 1;
#endif
                    hrefInfo.Name      = tag.Name;
                    hrefInfo.HrefValue = tag.Value;
                    _listHrefInfos.Add(hrefInfo);


                    break;
                }
                }
            }

/*
 *          _textBuilder.Clear();
 *
 *          foreach (Match match in _inputTagRegex.Matches(inputText))
 *          {
 *              int tempId = 0;
 *              if (!string.IsNullOrEmpty(match.Groups[1].Value) && !match.Groups[1].Value.Equals("-"))
 *                  tempId = int.Parse(match.Groups[1].Value);
 *              string tempTag = match.Groups[2].Value;
 *              var tagLength = tempTag.Length;
 *              //更新超链接
 *              if (tempId < 0)
 *              {
 *                  part = inputText.Substring(textIndex, match.Index - textIndex);
 *                  _textBuilder.Append(part);
 *
 *                  var lastLength = ReplaceRichText(_textBuilder.ToString()).Length;
 *                  int startIndex = (lastLength- vertSum)*4;;
 *                  _textBuilder.AppendFormat("<color=blue>{0}</color>", match.Groups[2].Value);
 *
 *                  vertSum += ReplaceRichText(_textBuilder.ToString()).Length - lastLength-tagLength;
 *                  int endIndex = startIndex + tagLength*4 ;
 *
 #if UNITY_2019_1_OR_NEWER
 *                  newIndex += ReplaceRichText(part).Length * 4;
 *                  int newStartIndex = newIndex;
 *                  newIndex += match.Groups[2].Value.Length * 4 + 8;
 #endif
 *
 *                  var hrefInfo = Pool<HrefInfo>.Get();
 *                  hrefInfo.Id = Mathf.Abs(tempId);
 *                  hrefInfo.StartIndex = startIndex;// 超链接里的文本起始顶点索引
 *                  hrefInfo.EndIndex = endIndex;
 #if UNITY_2019_1_OR_NEWER
 *                  hrefInfo.NewStartIndex = newStartIndex;
 *                  hrefInfo.NewEndIndex = newIndex - 1;
 #endif
 *                  hrefInfo.Name = match.Groups[2].Value;
 *                  hrefInfo.HrefValue = match.Groups[3].Value;
 *                  _listHrefInfos.Add(hrefInfo);
 *              }
 *              //更新表情
 *              else
 *              {
 *                  SpriteInfoGroup tempGroup = SpriteAssetManager.Instance.GetSpriteGroup(tempId, tempTag);
 *                  if (tempGroup == null)
 *                      continue;
 *                  part = inputText.Substring(textIndex, match.Index - textIndex);
 *                  _textBuilder.Append(part);
 *
 *
 *                  var lastLength = ReplaceRichText(_textBuilder.ToString()).Length;
 *
 #if UNITY_2019_1_OR_NEWER
 *                  newIndex += ReplaceRichText(part).Length * 4;
 #endif
 *                  tempIndex = (lastLength- vertSum)*4;
 *                  var s= fontSize/tempGroup.Size;
 *                  _textBuilder.AppendFormat("<quad size={0} width={1}/>", tempGroup.Size * s, tempGroup.Width);
 *                  vertSum += ReplaceRichText(_textBuilder.ToString()).Length - lastLength-1;
 *
 *                  //清理标签
 *                  SpriteTagInfo tempSpriteTag = Pool<SpriteTagInfo>.Get();
 *                  tempSpriteTag.Index = tempIndex;
 *
 #if UNITY_2019_1_OR_NEWER
 *                  tempSpriteTag.NewIndex = newIndex;
 #endif
 *
 *                  tempSpriteTag.Id = tempId;
 *                  tempSpriteTag.Tag = tempTag;
 *                  tempSpriteTag.Size = new Vector2(tempGroup.Size * tempGroup.Width, tempGroup.Size);
 *                  tempSpriteTag.UVs = tempGroup.ListSpriteInfor[0].Uv;
 *
 *                  //添加正则表达式的信息
 *                  SpriteTagInfoDic.TryGetValue(tempSpriteTag.Id, out var list);
 *                  if (list == null)
 *                  {
 *                      if (SpriteTagInfoDic.ContainsKey(tempSpriteTag.Id))
 *                      {
 *                          SpriteTagInfoDic.Remove(tempSpriteTag.Id);
 *                      }
 *
 *                      list = new List<SpriteTagInfo>();
 *                      SpriteTagInfoDic.Add(tempSpriteTag.Id,list);
 *                  }
 *                  list.Add(tempSpriteTag);
 *                  SpriteEmoji spriteEmoji = GetSpriteEmoji(tempId);
 *                  spriteEmoji.m_spriteAsset =SpriteAssetManager.Instance.GetSpriteAsset(tempId);
 *                  spriteEmoji.material.mainTexture = SpriteAssetManager.Instance.GetSpriteTexture(tempId);
 *                  spriteEmoji.ClearEmojiVert();
 *                  spriteEmoji.SetAllDirty();
 #if UNITY_2019_1_OR_NEWER
 *                  newIndex += 4;
 #endif
 *              }
 *
 *
 *              textIndex = match.Index + match.Length;
 *          }
 *
 *          _textBuilder.Append(inputText.Substring(textIndex, inputText.Length - textIndex));
 */
            return(_textBuilder.ToString());
        }