参考链接:

https://github.com/SylarLi/RichText/tree/master/Assets/Scripts

正则表达式:

https://blog.csdn.net/lyh916/article/details/49201195

图文混排主要用于聊天,其实就是传输某种格式的字符串,然后解析这个字符串,生成表情文字等。图文混排的第一步,就是确定好格式,这里使用html的标签格式,对于代码中出现的start和end字段可以先忽略。标签格式如下:

<material=underline c=#ffffff h=1 n=*** p=***>blablabla...</material>

RichTextTag.cs

 //标签类型
public enum RichTextTagType
{
None,
Underline, //下划线
} //标签基类
public abstract class RichTextTag
{
public int start;
public int end;
public abstract RichTextTagType tagType { get; } public abstract void SetValue(string key, string value);
}

RichTextUnderlineTag.cs

 using UnityEngine;

 //下划线标签
public class RichTextUnderlineTag : RichTextTag { public Color color = Color.white;//颜色
public float height = 1f;//高度
public string eventName;//事件名
public string eventParameter;//事件参数 public override RichTextTagType tagType { get { return RichTextTagType.Underline; } } public override void SetValue(string key, string value)
{
switch (key)
{
case "c":
{
ColorUtility.TryParseHtmlString(value, out color);
break;
}
case "h":
{
float.TryParse(value, out height);
break;
}
case "n":
{
eventName = value;
break;
}
case "p":
{
eventParameter = value;
break;
}
default:
break;
}
}
}

RichTextTagParser.cs

 using System.Text.RegularExpressions;

 //解析一条标签:<material xxx>
public class RichTextTagParser { private static readonly Regex tagRegex = new Regex(@"<material=([^>\s]+)([^>]*)>");//(标签类型)(标签参数)
private static readonly Regex paraRegex = new Regex(@"(\w+)=([^\s]+)");//(key)=(value)
public string content;//<material=xxx xxx>
public int start;
public int end; public RichTextTag Parse()
{
RichTextTag tag = null;
Match match = tagRegex.Match(content);
if (match.Success)
{
string tagName = match.Groups[].Value;//标签类型
if (!tagName.StartsWith("#"))
{
var keyValueCollection = paraRegex.Matches(match.Groups[].Value);//标签参数
switch (tagName)
{
case "underline":
{
tag = new RichTextUnderlineTag();
break;
}
default:
break;
}
if (tag != null)
{
tag.start = start;
tag.end = end;
for (int i = ; i < keyValueCollection.Count; i++)
{
string key = keyValueCollection[i].Groups[].Value;
string value = keyValueCollection[i].Groups[].Value;
tag.SetValue(key, value);
}
}
}
}
return tag;
}
}

RichTextParser.cs

 using System.Collections.Generic;
using System.Text.RegularExpressions; //解析全部标签
public class RichTextParser { private static readonly Regex regex = new Regex(@"</*material[^>]*>");//<material xxx> or </material>
private const string endStr = "</material>"; private Stack<RichTextTagParser> tagParserStack;
private List<RichTextTag> tagList; public RichTextParser()
{
tagParserStack = new Stack<RichTextTagParser>();
tagList = new List<RichTextTag>();
} public void Parse(string richText, out List<RichTextTag> tags)
{
tagParserStack.Clear();
tagList.Clear();
Match match = regex.Match(richText);
while (match.Success)
{
if (match.Value == endStr)
{
if (tagParserStack.Count > )
{
RichTextTagParser tagParser = tagParserStack.Pop();
tagParser.end = match.Index - ;
if (tagParser.end >= tagParser.start)
{
RichTextTag tag = tagParser.Parse();
if (tag != null)
{
tagList.Add(tag);
}
}
}
}
else
{
RichTextTagParser tagParser = new RichTextTagParser();
tagParser.content = match.Value;
tagParser.start = match.Index + match.Length;
tagParserStack.Push(tagParser);
}
match = match.NextMatch();
}
tags = tagList;
}
}

测试如下:

 using UnityEngine;
using System.Collections.Generic; //下划线<material=underline c=#ffffff h=1 n=*** p=***>blablabla...</material>
public class RichTextTest : MonoBehaviour { private string a = @"123<material=underline c=#ff0000 h=1 n=name1 p=para1>blablabla...</material>qwe" +
@"<material=underline c=#00ff00 h=2 n=name2 p=para2>blablabla...</material>asd";
private List<RichTextTag> tagList; private void Start()
{
RichTextParser parser = new RichTextParser();
parser.Parse(a, out tagList);
for (int i = ; i < tagList.Count; i++)
{
RichTextUnderlineTag tag = tagList[i] as RichTextUnderlineTag;
Debug.Log(tag.color);
Debug.Log(tag.height);
Debug.Log(tag.eventName);
Debug.Log(tag.eventParameter);
}
}
}

结果:

[UGUI]图文混排(一):标签制定和解析的更多相关文章

  1. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  2. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  3. Unity UGUI图文混排源码(一)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  4. Unity琐碎(3) UGUI 图文混排解决方案和优化

    感觉使用Unity之后总能看到各种各样解决混排的方案,只能说明Unity不够体恤下情啊.这篇文章主要讲一下个人在使用过程中方案选择和优化过程,已做记录.顺便提下,开源很多意味着坑,还是要开实际需求. ...

  5. [UGUI]图文混排(二):Text源码分析

    UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...

  6. Unity UGUI图文混排(七) -- 下划线

    之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...

  7. Unity UGUI图文混排(六) -- 超链接

    图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...

  8. [UGUI]图文混排(三):资源管理

    1.图文混排中的资源,主要是图片. 2.所谓的资源管理,可以分为资源对象池和资源加载这两部分.这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理. RichTextResource ...

  9. Unity UGUI图文混排(五) -- 一张图集对应多个Text

    继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...

随机推荐

  1. cmp命令详解

    Linux cmp命令 Linux cmp命令用于比较两个文件是否有差异.主要用来比较两个二进制文件 当相互比较的两个文件完全一样时,则该指令不会显示任何信息.若发现有所差异,预设会标示出第一个不同之 ...

  2. 有10 亿个 url,每个 url 大小小于 56B,要求去重,内存只给你4G

    问题:有10 亿个 url,每个 url 大小小于 56B,要求去重,内存只给你4G 思路: 1.首先将给定的url调用hash方法计算出对应的hash的value,在10亿的url中相同url必然有 ...

  3. 小米笔记本装MAC(黑苹果),外接USB蓝牙,禁止笔记本自带的蓝牙方法

    https://blog.daliansky.net/ 解决插入耳机没有声音问题,每次开机需要重新插拔一次:https://github.com/daliansky/XiaoMi-Pro/blob/m ...

  4. RTTI D7

    RTTI需要引用单元TypeInfo 至于RTTI的数据结构,大家可以参考TypeInfo单元的代码 看例子,先为大家介绍一下根据字符串找到属性,并且对其修改的例子 根据属性字符串找到属性,并修改属性 ...

  5. TStrings与Memo.Lines赋值的问题

    //想实现在函数中生成Memo1的内容,而后赋给Memo.Lines //方法1: var s: TStrings; begin s := TStringList.Create; AddMemoDat ...

  6. IntelliJ Idea 跳出括号并且光标移到末尾的快捷键

    直接跳出的shift enter不管现在光标在哪个位置,直接新开一行 跳出双引号:shift + "跳出单引号:'跳出括号:shift + )跳出中括号:]以此类推.

  7. JS一行代码,生成一个16进制随机颜色,简单粗暴。

    var color = '#'+ Math.random().toString(16).substr(-6); document.body.style.backgroundColor = color; ...

  8. Underscore.js部分讲解

    underscore是非常好用的封装库,大小只有4KB,大多插件都是以underscore为基础: underscore分5大部分:集合:数组:函数:对象:工具 集合:集合就是伪数组,虽然长的和数组一 ...

  9. es中的一些知识点记录

    1. forcemerge接口 强制段合并,设置为1时,是期望最终只有1个索引段.但实际情况是,合并的结果是段的总数会减少,但仍大于1,可以多次执行强制合并的命令. 设置的的目标值越小.合并消耗的时间 ...

  10. qsort实现结构体数组排序

    要注意强制转换 #include <stdio.h> #include <stdlib.h> typedef struct{ int num; char name[20]; f ...