[UGUI]图文混排(一):标签制定和解析
参考链接:
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]图文混排(一):标签制定和解析的更多相关文章
- Unity UGUI图文混排源码(三) -- 动态表情
这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...
- Unity UGUI图文混排源码(二)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...
- Unity UGUI图文混排源码(一)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...
- Unity琐碎(3) UGUI 图文混排解决方案和优化
感觉使用Unity之后总能看到各种各样解决混排的方案,只能说明Unity不够体恤下情啊.这篇文章主要讲一下个人在使用过程中方案选择和优化过程,已做记录.顺便提下,开源很多意味着坑,还是要开实际需求. ...
- [UGUI]图文混排(二):Text源码分析
UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...
- Unity UGUI图文混排(七) -- 下划线
之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...
- Unity UGUI图文混排(六) -- 超链接
图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...
- [UGUI]图文混排(三):资源管理
1.图文混排中的资源,主要是图片. 2.所谓的资源管理,可以分为资源对象池和资源加载这两部分.这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理. RichTextResource ...
- Unity UGUI图文混排(五) -- 一张图集对应多个Text
继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...
随机推荐
- redis : 桌面管理工具 redis-desktop-manager使用指南
概要:一款好用的Redis桌面管理工具,支持命令控制台操作,以及常用,查询key,rename,delete等操作. 下载软件,请点击下面链接,进入下载页,选择对应版本: https://redisd ...
- Kafka Stream
Kafka Stream是Apache Kafka从0.10版本引入的一个新Feature(当前:1.0.0-rc0,参见:https://github.com/apache/kafka/releas ...
- GTP+SDI工程播出部分思路整理(3)
GTP+SDI工程播出部分思路整理(3) 1.本文的目的主要分析video_out_to_sdi模块中输入信号 tx_usrclk, rst, tx_mode, tx_level_b的使用 Tx_us ...
- Pandas的使用(1)
Pandas的使用(1) 1.绘图 import pandas as pd import numpy as np import matplotlib.pyplot as plt ts = pd.Ser ...
- Ubuntu 14.10 下安装rabbitvcs-版本控制
在Windows下用惯了TortoiseSVN这只小乌龟,到了Ubuntu下很不习惯命令行的SVN,于是经过一番寻找安装了RabbitVCS这款SVN图形化前端工具(官方网站:http://rabbi ...
- CodeLite C/C+ IDE更新放出
CodeLite IDE Revision 1145 for C/C++已经发布,这是一个强大的开源,跨平台的C/C++整合开发环境.目前已经在Windows XP SP3,Ubuntu 7.10 G ...
- SQL注入漏洞解决方法
本文只指针编码层次的SQL注入漏洞解决方法,例子代码是以java为主. 1,参数化的预编译查询语句 不安全例子 String query = "SELECT account_balance ...
- PLSQL导出对象的表结构和表数据
https://jingyan.baidu.com/article/fcb5aff78e6a48edab4a7146.html
- PTA2016天梯赛决赛3小时训练赛
7-2 I Love GPLT (5 分) 这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— I Love GPLT ——竖着输出就可以了. 所谓“竖着输出”,是指每个字符占一行(包括 ...
- 知识点:Mysql 基本用法之事务
事务 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 事务实例: create table user( id int primar ...