首页 > 其他分享 >ugui学习 - 字形度量, 文本排版

ugui学习 - 字形度量, 文本排版

时间:2023-08-12 23:56:41浏览次数:36  
标签:Log textGen text var Debug ugui 排版 度量

Text的文本排版是通过TextGenerator来实现的,它把排版的细节封装在c++层了,我们无法看到,但可以在c#层获取到排版后的详细信息,包括:

每个字形(Glyph)的排版信息,行的排版信息等。

通过把排榜后的信息打印出来,我们就可以大致了解排版的原理

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class TestTextGen : MonoBehaviour
{

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            var text = GetComponent<Text>();
            PrintFontInfo(text.font);
            Debug.Log($"unitPerPixel: {1 / text.pixelsPerUnit}, {text.pixelsPerUnit}");

            var textGen = text.cachedTextGenerator;
            PrintTextGenInfo(text.text, textGen);
        }
    }

    static void PrintFontInfo(Font f)
    {
        Debug.Log($"===== font:{f.name}");
        Debug.Log($"fontSize:{f.fontSize}, dynamic:{f.dynamic}, ascent:{f.ascent}, lineHeight:{f.lineHeight}");

#if UNITY_EDITOR
        SerializedObject so = new SerializedObject(f);
        float fontSize = so.FindProperty("m_FontSize").floatValue;
        float ascent = so.FindProperty("m_Ascent").floatValue;
        float lineSpacing = so.FindProperty("m_LineSpacing").floatValue;
        float descent = 0;
        SerializedProperty sp_Descent = so.FindProperty("m_Descent");
        if (sp_Descent != null)
            descent = sp_Descent.floatValue;
        Debug.Log($"fontSize:{fontSize}, ascent:{ascent}, lineSpacing:{lineSpacing}, descent:{descent}");
#endif

        var fontMat = f.material;
        if (f.material)
        {
            Texture fontTex = fontMat.mainTexture;
            if (fontTex)
                Debug.Log($"texture:{fontTex.name}, size:({fontTex.width}, {fontTex.height})");
            else
                Debug.Log($"no font Texture");
        }
        else
        {
            Debug.Log($"no font mat");
        }

        Debug.Log($"=====");
    }

    static void PrintTextGenInfo(string text, TextGenerator textGen)
    {
        Debug.Log($"========== TextGenInfo");

        var chs = textGen.characters;
        Debug.Log($"charCount:{chs.Count}, {textGen.characterCount}, visible:{textGen.characterCountVisible}");
        for (int i = 0; i < chs.Count; ++i)
        {
            var ch = chs[i];
            if (i < text.Length)
                Debug.Log($"ch_{i}: '{text[i]}', w:{ch.charWidth}, cursoPos:{ch.cursorPos.ToStr()}");
            else
                Debug.Log($"ch_{i}: w:{ch.charWidth}, cursoPos:{ch.cursorPos.ToStr()}");
        }

        //cursorPos和verts_lt的坐标区别是: cursorPos不会超出Text限定框、不会字符重叠, y方向是贴着lineTop,
        var verts = textGen.verts;
        Debug.Log($"vertCount:{verts.Count}, {textGen.vertexCount}");
        for (var i = 0; i < verts.Count; i += 4)
        {
            var lt = verts[i].position;
            var rt = verts[i + 1].position;
            var rb = verts[i + 2].position;
            var rl = verts[i + 3].position;
            Debug.Log($"quad_{i/4}: lt:{lt.ToStr()}, rt:{rt.ToStr()}, rb:{rb.ToStr()}, rl:{rl.ToStr()}");
        }

        var lines = textGen.lines;
        Debug.Log($"lineCount:{lines.Count}, {textGen.lineCount}");
        float lastLineBottomY = 0;
        for (int i = 0; i < lines.Count; ++i)
        {
            var l = lines[i];
            float bottomY = l.topY - l.height;
            if (i > 0)
            {
                float leading = l.topY - lastLineBottomY;
                Debug.Log($"line_{i}: charIndex:{l.startCharIdx}, lineLeading:{l.leading}_{leading}, lineHeight:{l.height}, line_topY:{l.topY}, line_bottomY:{bottomY}");
            }
            else
            {
                Debug.Log($"line_{i}: charIndex:{l.startCharIdx}, lineLeading:{l.leading}, lineHeight:{l.height}, line_topY:{l.topY}, line_bottomY:{bottomY}");
            }
            lastLineBottomY = bottomY;
        }

        Debug.Log($"==========");
    }

}

 

排版示例1

绿色的线为行top, 蓝色的线为行bottom, 红色的线为基线

1) ascent: 行top位置往下移动ascent的距离就是基线所在位置

2) descent: 基线位置往下移动descent的距离就是行bottom所在的位置

3) line height: 行top和行bottom间的距离就是行高

4) line leading: 下一行top和上一行bottom间的距离就是行间距

 

排版示例2

 

标签:Log,textGen,text,var,Debug,ugui,排版,度量
From: https://www.cnblogs.com/sailJs/p/17514700.html

相关文章

  • .NET 个人博客-首页排版优化-2
    个人博客-首页排版优化-2原本这篇文章早就要出了的,结果之前买的服务器服务商跑路了,导致博客的数据缺失了部分。我是买了一年的服务器,然后用了3个月,国内跑路云太多了,然后也是花钱重新去别的服务商买了一台服务器,这次只买了一个月,先试试水。优化计划置顶3个且可滚动或切换推......
  • HTML | HTML排版标签
    标签名标签含义单/双标签h1~h6标题双p段落双div没有任何含义,用于整体布局(生活中的包装袋)。双h1最好写一个,h2~h6能适当多写。h1~h6不能互相嵌套,例如:h1标签中最好不要写h2标签了。p标签很特殊!它里面不能有:h1~h6、p、div标签(暂......
  • 官宣 | CODING 荣获可信云软件研发效能度量平台先进级最高评估成果
    点击链接了解详情2023年7月25日,由中国信息通信研究院(以下简称“中国信通院”)和中国通信标准化协会联合主办的2023可信云大会在北京召开。大会以“云领创新算启新篇”为主题,发布了《云计算白皮书(2023年)》《中国算力服务研究报告(2023年)》,解读了2023云计算十大关键词,......
  • go 语言实战入门案例之命令行排版词典
    文章和代码已经归档至【Github仓库:<https://github.com/timerring/backend-tutorial>】或者公众号【AIShareLab】回复go也可获取。命令行排版的词典先看一下用到的API,以彩云科技提供的在线翻译为例。请打开彩云翻译的网页,然后右键检查打开浏览器的开发者工具。此时我们点一下......
  • ugui学习 - PC键盘输入
    最终效果 实现的功能a)点击后显示了光标,才能输入b)按esc,退出输入,光标会隐藏c)输入时,内容加在光标后面d)Backspace删除光标后的字符e)Delete删除光标前的字符usingUnityEngine;usingUnityEngine.EventSystems;usingUnityEngine.UI;publicclassMyI......
  • ugui学习 - 自己实现InputField鼠标点击调整光标位置,拖拽修改选中区域
    效果 代码usingUnityEngine;usingUnityEngine.EventSystems;usingUnityEngine.UI;publicclassMyInputField_CaretDragOp:MyInputField_TextSelect,IDragHandler,IPointerDownHandler{publicvoidOnPointerDown(PointerEventDataeventData){......
  • ugui学习 - 自己实现InputField的文本选中
    效果 代码把光标闪烁拿掉了usingUnityEngine;usingUnityEngine.UI;[DisallowMultipleComponent][RequireComponent(typeof(CanvasRenderer))][RequireComponent(typeof(RectTransform))]publicclassMyInputField_TextSelect:MonoBehaviour,ICanvasElement{......
  • Unity UGUI的Shadow(阴影)组件的介绍及使用
    UnityUGUI的Shadow(阴影)组件的介绍及使用1.什么是Shadow(阴影)组件?Shadow(阴影)组件是UnityUGUI中的一个特效组件,用于在UI元素上添加阴影效果。通过调整阴影的颜色、偏移、模糊等属性,可以使UI元素看起来更加立体和有层次感。2.Shadow(阴影)组件的工作原理Shadow(阴影)组件......
  • Unity UGUI的Outline(描边)组件的介绍及使用
    UnityUGUI的Outline(描边)组件的介绍及使用1.什么是Outline(描边)组件?Outline(描边)组件是UnityUGUI中的一种特效组件,用于给UI元素添加描边效果。通过设置描边的颜色、宽度和模糊程度,可以使UI元素在视觉上更加突出。2.Outline(描边)组件的工作原理Outline(描边)组件通过在U......
  • Unity UGUI的Outline(描边)组件的介绍及使用
    UnityUGUI的Outline(描边)组件的介绍及使用1.什么是Outline(描边)组件?Outline(描边)组件是UnityUGUI中的一种特效组件,用于给UI元素添加描边效果。通过设置描边的颜色、宽度和模糊程度,可以使UI元素在视觉上更加突出。2.Outline(描边)组件的工作原理Outline(描边)组件通过在......