首页 > 编程语言 >Unity UGUI图文混排源码(一)

Unity UGUI图文混排源码(一)

时间:2022-12-13 15:06:56浏览次数:74  
标签:text preferredHeight iNum linesInfo value Unity 源码 Text UGUI



我从一开始想到的图文混排的概念都是通过文字间的空隙去粘贴一张图片,这样确定图片前面文字的最后一个位置变成了最主要的参数,接下来就给出两种解决方案

首先,先发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;
}
}

标签:text,preferredHeight,iNum,linesInfo,value,Unity,源码,Text,UGUI
From: https://blog.51cto.com/u_15911199/5934132

相关文章

  • Unity UGUI基础之Text
    Text作为UGUI最基础的控件以及最常用的控件,它在项目中的应用绝对可以算是最多的,任何一个UI界面可以说都离不开它,它的基本属性如下:一、recttransform组件:recttransform(矩形......
  • Unity UGUI实现图文混排
    目前在unity实现图文混排的好像都是通过自定义字体然后在文本获取字符的位置,用图片替换掉图片标签,这样对于支持英文来说,并没有什么影响。然后对于中文来说就是一个相当麻烦......
  • Unity插件 - MeshEditor(十) 模型风力拉扯特效
    更新日期:2020年4月23日。 Github源码:​​​[点我获取源码]​​ 先上几张效果图:  (导演:我们需要一个刮风的效果,道具组,上大风扇) (导演:咔!!!行了,道具组你们明天不用来上班了) ......
  • Unity 纹理优化及TextureProcessor工具
    更新日期:2020年11月18日。Github源码:​​​[点我获取源码]​​索引​​纹理优化​​​​TextureProcessor工具​​​​使用​​​​Resizer纹理缩放器​​​​打开纹理缩放......
  • Unity插件 - MeshEditor(十一) 模型正弦扭曲特效
    更新日期:2020年4月23日。Github源码:​​​[点我获取源码]​​ 先上几张效果图:   OK,进入今天的正题吧,插一个正弦函数的话题进来: 首先,正弦函数曲线,如下:  在如上坐标系......
  • 【Unity】 HTFramework框架(三十九)UI的数据驱动模式,MVVM
    更新日期:2020年10月24日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​UI的数据驱动模式​​​​使用​​​​数据模型​​​​Bindabl......
  • Unity UGUI无限列表(Infinite List)
    更新日期:2020年10月16日。Github源码:​​​[点我获取源码]​​索引​​InfiniteList​​​​使用​​​​创建InfiniteListScrollRect​​​​InfiniteListScrollRect参数......
  • Unity - 粒子系统跟随路径移动
    对于最新版的粒子系统ParticleSystem,要让其跟随路径移动,无非就是借用其自身的API直接为每个粒子设置速度。看一下最终的效果图:编辑器为了能在场景中更方便的编辑路径,我们要......
  • 【Unity】 HTFramework框架(三十七)Inspector自定义序列化检视器
    更新日期:2020年8月21日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​Inspector自定义序列化检视器​​​​使用​​​​Dropdown下拉......
  • 【Unity】 HTFramework框架(三十六)AssetsMaster资源管理器,做资产的主人
    更新日期:2020年8月13日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​AssetsMaster​​​​使用AssetsMaster​​​​打开AssetsMaster......