Unity UGUI图文混排源码(一)
Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304
Unity UGUI图文混排源码(二):http://blog.csdn.net/qq992817263/article/details/51112311
我从一开始想到的图文混排的概念都是通过文字间的空隙去粘贴一张图片,这样确定图片前面文字的最后一个位置变成了最主要的参数,接下来就给出两种解决方案
首先,先发UGUI源码的一个链接,很多东西可以参考他本身代码和一些可获取的属性:https://bitbucket.org/Unity-Technologies/ui
图文混排解决方案一:
通过获取preferredWidth和preferredHeight来确定他的位置,再通过空格,给图片留下位置,这里只提一下关键点,具体建议使用第二种解决方案
1.通过自定义标签,将文本中的文字分为几段
void DealTextInfor(string value)
{
if (m_spriteAsset == null)
return;
if (m_spriteAsset.listSpriteInfor.Count <= 0)
return; if (value.Contains("<sprite="))
{
//获取到<sprite前面的所有内容 并确定最后一个字符的位置信息 再+四个空格 赋值给m_text
int iNum = value.IndexOf("<sprite=");
m_text.text += value.Substring(0, iNum);
Debug.Log("m_Text preferredWidth:" + m_text.preferredWidth + " m_Text preferredHeight:" + m_text.preferredHeight);
//Debug.Log("m_Text line count:" + m_TextGenerator.GetLinesArray().Length);
UILineInfo[] linesInfo = m_textGenerator.GetLinesArray();
for (int i = 0; i < linesInfo.Length; i++)
{
Debug.Log("start char Index:" + linesInfo[i].startCharIdx + "start char:" + m_text.text.ToCharArray()[linesInfo[i].startCharIdx] + " line height:" + linesInfo[i].height);
}
Vector3 textpos = Vector3.zero;
int startCharIdx = linesInfo[linesInfo.Length - 1].startCharIdx;
textpos.x = m_textGenerator.GetPreferredWidth(m_text.text.Substring(startCharIdx, m_text.text.Length - startCharIdx), m_textSetting);
textpos.y = -(m_text.preferredHeight);
Debug.Log("text pos:" + textpos); m_text.text += " ";
//获取到精灵索引的ID 并去掉精灵标签
value = value.Substring(iNum, value.Length - iNum);
Debug.Log("value:" + value);
iNum = value.IndexOf("/>");
int iSpriteIndex = int.Parse(value.Substring(0, iNum).Trim().Replace("<sprite=", ""));
SaveSpriteInfor(textpos, m_textGenerator.GetPreferredWidth(" ", m_textSetting), iSpriteIndex);
Debug.Log("sprite index:" + iSpriteIndex); value = value.Substring(iNum + 2, value.Length - iNum - 2);
DealTextInfor(value);
}
else
{
m_text.text += value;
DrawSprite();
}
}
2.将当前图片的前面的几段文字集合,通过获取这段文字的preferredWidth和preferredHeight来确定他的位置,这个局限性就在于文本最处于左上角或者左下角的排列位置,才能方便计算,同时如果文字的字体差异较大之后,preferredWidth和preferredHeight的位置并不能体现很好的效果
3.获取preferredWidth和preferredHeight的方式,可以通过Text自身的属性去直接获取,也可以通过代码去计算
Text m_Text = GetComponent<Text>(); TextGenerator m_TextGenerator = m_Text.cachedTextGeneratorForLayout;
TextGenerationSettings m_TextGenerationSettings = m_Text.GetGenerationSettings(Vector2.zero);
float fWidth = m_TextGenerator.GetPreferredWidth("一段文字,获取所占宽度", m_TextGenerationSettings); m_TextGenerationSettings = m_Text.GetGenerationSettings(new Vector2(m_Text.rectTransform.rect.x,0.0f));
float fHeight = m_TextGenerator.GetPreferredHeight("一段文字,获取所占高度", m_TextGenerationSettings);
4.参考源码计算的方式
public virtual float preferredWidth
{
get
{
var settings = GetGenerationSettings(Vector2.zero);
return cachedTextGeneratorForLayout.GetPreferredWidth(m_Text, settings) / pixelsPerUnit;
}
}
public virtual float preferredHeight
{
get
{
var settings = GetGenerationSettings(new Vector2(rectTransform.rect.size.x, 0.0f));
return cachedTextGeneratorForLayout.GetPreferredHeight(m_Text, settings) / pixelsPerUnit;
}
}
5.具体的表现效果参考上一篇文章:http://blog.csdn.net/qq992817263/article/details/51000744
Unity 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图文混排源码(四) -- 聊天气泡
这里有同学建议在做聊天气泡时,可以更改为一张图集对应多个Text,这样能节省资源,不过我突然想到每个Text一个图集,可以随时更换图集,这样表情图更丰富一些,于是我就先将现有的聊天demo改为了聊天气 ...
- Unity UGUI图文混排(六) -- 超链接
图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...
- Unity UGUI图文混排(七) -- 下划线
之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...
- Unity UGUI图文混排(五) -- 一张图集对应多个Text
继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...
- Unity琐碎(3) UGUI 图文混排解决方案和优化
感觉使用Unity之后总能看到各种各样解决混排的方案,只能说明Unity不够体恤下情啊.这篇文章主要讲一下个人在使用过程中方案选择和优化过程,已做记录.顺便提下,开源很多意味着坑,还是要开实际需求. ...
- [UGUI]图文混排(二):Text源码分析
UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...
- [UGUI]图文混排(三):资源管理
1.图文混排中的资源,主要是图片. 2.所谓的资源管理,可以分为资源对象池和资源加载这两部分.这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理. RichTextResource ...
随机推荐
- ubuntu 卸载从源码安装的 emacs
由于配置问题想卸了重装. 解压并进入你的源码所在目录: ./configure sudo make uninstall Done Reference: http://askubuntu.com/que ...
- python学习之路前端-JavaScript
JavaScript简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本 ...
- 初始化mysql数据库——Activiti BPM
package com.initialize; import org.activiti.engine.ProcessEngine; import org.activiti.engine.Process ...
- 聊聊jstack的工作原理
实现一个jstack 在聊Jstack得工作原理前呢,不如让我们先写一个简单的jstack玩玩.不用怕,很简单的,就几行代码的事,看: public class MyJstack { public s ...
- 为什么要用 Docker
作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势. 首先,Docker 容器的启动可以在秒级实现,这相比传统的虚拟机方式要快得多. 其次,Docker 对系统资源的利用率很 ...
- Linux下文件的mtime/atime/ctime研究
概述 在Linux下,对于某一个文件或文件夹时间的描述有三种:文件修改时间mtime,文件访问时间atime,文件状态改变时间ctime.在Linux下无法获取到文件的创建时间,因为根本就没有保存这个 ...
- CentOS 7 下使用虚拟环境Virtualenv安装Tensorflow cpu版记录
1.首先安装pip-install 在使用centos7的软件包管理程序yum安装python-pip的时候会报一下错误: No package python-pip available. Error ...
- Matplotlib Toolkits:python高级绘图库seaborn
http://blog.csdn.net/pipisorry/article/details/49515745 Seaborn介绍 seaborn (Not distributed with matp ...
- Memcached - In Action
Memcached 标签 : Java与NoSQL With Java 比较知名的Java Memcached客户端有三款:Java-Memcached-Client.XMemcached以及Spym ...
- 磁盘管理,磁盘挂在mount,挂载光盘镜像文件,挂在U盘,umount 卸载命令, dd
1 mount 命令格式: mount[-t vfstype] -o options device dir 其中: *-t vfstype 指定文件系统的类型,通常不必指定.mount会自动选择正确的 ...