当前位置:网站首页>Unity ugui text text box adaptation
Unity ugui text text box adaptation
2022-07-27 01:24:00 【Calm nine】
Unity UGUI Text Text box adaptation
One 、 The size of the text box is fixed , Text adaptation
1、 Check the of the text box Best Fit, It is found that the effect is not particularly ideal , When there is more text, you should wrap first , The font will not shrink until it is full , If there are spaces in the text , It seems that it can
2、 Customize text
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Text
/// Auto shrink
/// Check Best Fit after , The font size will be reduced only after exceeding the text box
/// </summary>
public class ShrinkText : Text
{
/// <summary>
/// Number of currently visible text lines
/// </summary>
public int VisibleLines { get; private set; }
private void _UseFitSettings()
{
TextGenerationSettings settings = GetGenerationSettings(rectTransform.rect.size);
settings.resizeTextForBestFit = false;
if (!resizeTextForBestFit)
{
cachedTextGenerator.PopulateWithErrors(text, settings, gameObject);
return;
}
int minSize = resizeTextMinSize;
int txtLen = text.Length;
for (int i = resizeTextMaxSize; i >= minSize; --i)
{
settings.fontSize = i;
cachedTextGenerator.PopulateWithErrors(text, settings, gameObject);
if (cachedTextGenerator.characterCountVisible == txtLen) break;
}
}
private readonly UIVertex[] _tmpVerts = new UIVertex[4];
protected override void OnPopulateMesh(VertexHelper toFill)
{
if (null == font) return;
m_DisableFontTextureRebuiltCallback = true;
_UseFitSettings();
// Apply the offset to the vertices
IList<UIVertex> verts = cachedTextGenerator.verts;
float unitsPerPixel = 1 / pixelsPerUnit;
int vertCount = verts.Count;
// We have no verts to process just return (case 1037923)
if (vertCount <= 0)
{
toFill.Clear();
return;
}
Vector2 roundingOffset = new Vector2(verts[0].position.x, verts[0].position.y) * unitsPerPixel;
roundingOffset = PixelAdjustPoint(roundingOffset) - roundingOffset;
toFill.Clear();
if (roundingOffset != Vector2.zero)
{
for (int i = 0; i < vertCount; ++i)
{
int tempVertsIndex = i & 3;
_tmpVerts[tempVertsIndex] = verts[i];
_tmpVerts[tempVertsIndex].position *= unitsPerPixel;
_tmpVerts[tempVertsIndex].position.x += roundingOffset.x;
_tmpVerts[tempVertsIndex].position.y += roundingOffset.y;
if (tempVertsIndex == 3)
toFill.AddUIVertexQuad(_tmpVerts);
}
}
else
{
for (int i = 0; i < vertCount; ++i)
{
int tempVertsIndex = i & 3;
_tmpVerts[tempVertsIndex] = verts[i];
_tmpVerts[tempVertsIndex].position *= unitsPerPixel;
if (tempVertsIndex == 3)
toFill.AddUIVertexQuad(_tmpVerts);
}
}
m_DisableFontTextureRebuiltCallback = false;
VisibleLines = cachedTextGenerator.lineCount;
}
}
Two 、 The size of the text box changes with the text content
1、 add to Content Size Fitter Components , Choose horizontal variable or vertical variable according to your needs , The corresponding Fit Change it to Perferred Size
3、 ... and 、 The size of the text box with background changes with the text content
1、 stay Text Add Content Size Fitter and Vertical Layout Group Components 
2、Text Can't check Best Fit
3、 Adjust the parent object Content Size Fitter and Vertical Layout Group Components to achieve the desired effect
Reference resources
[Unity] UGUI expand - Better text scaling ShrinkText - Simple books (jianshu.com)
(2 Bar message ) Unity -UGUI in Text Automatic adjustment of text box , Adaptive adjustment of font size _ Guoyun's blog -CSDN Blog _ugui Text adaptation 
边栏推荐
- What is the digital economy and how does it change the business model?
- 快来帮你三分钟了解物联网
- Verilog过程赋值语句
- 做题笔记1
- Longest common substring
- 5. Legal bracket string
- Li Hongyi machine learning (2017 Edition)_ P14: back propagation
- New experience of mlvb cloud live broadcast: millisecond low latency live broadcast solution (with live broadcast performance comparison)
- The shortest way to realize video applets: from bringing goods to brand marketing
- The setup of KEIL development environment is delivered to the installation package
猜你喜欢

MySQL字符集设置为UTF-8,但控制台仍然出现中文乱码问题

4. Root user login

Jenkins--基础--5.1--系统配置--插件管理

What is the digital economy and how does it change the business model?

c语言实现扫雷游戏:

MySQL关闭连接事务自动提交的问题

Create MDK project

Unity CharacterController

Next generation Internet: Video Networking

The setup of KEIL development environment is delivered to the installation package
随机推荐
The setup of KEIL development environment is delivered to the installation package
markdown
Unity 使用NavMesh实现简易的摇杆功能
Six ways for the Internet of things to improve our lives
做题笔记1
ks 怎么抓salt值?api,did?
Square root of X
SQL learning (1) - table related operations
报错信息 WARNING: IPv4 forwarding is disabled. Networking will not work.
数据库期中(一)
RS485信号的测量
4. European Champions League
6. 世界杯来了
Jenkins -- Basic -- 5.2 -- system configuration -- system configuration
Jenkins--基础--5.2--系统配置--系统配置
[CTF attack and defense world] questions about backup in the web area
堆排序相关知识总结
Doris or starrocks JMeter pressure measurement
How can Tencent cloud live plug-in mlvb make use of these advantages to become the divine assistant of anchor Live Streaming?
基于485总线的评分系统