Unity笔记之UGUI Text首行缩进、改变文本颜色、文本间距调节

需求一:text首行需要缩进以达到下图的效果

在这里插入图片描述
实现举例:

string context = "\u3000\u3000全省新增境外输入确诊病例1例, 广州报告,来自柬埔寨;另有境外输入无症状感染者转确诊病例1例, 广州报告,来自柬埔寨。新增境外输入无症状感染者5例, 广州报告2例,分别来自美国和刚果金;佛山报告2例,分别来自美国和吉布提;肇庆报告1例,来自乌干达。新增出院3例。";

方法二:在属性面板里面放入这段话,把缩进改一下(这个其实是改变颜色的,这里的意思是把缩进两个字的颜色弄成了透明)(自己把空格删掉哈)

<color=#FFFFFF00>缩进</color>

需求二:text实现通过代码改变某一些字的颜色,可自行改变#FFFFFF00的值来达到想要的颜色(代码同理)(自己把空格删掉哈)

<color=#FFFFFF00>需要改变颜色的内容</color>

无意间发现的这篇博客写的挺好的

需求三:实现text内容间距的调节

// An highlighted block
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Line
{

    private int _startVertexIndex = 0;
    /// <summary>
    /// 起点索引
    /// </summary>
    public int StartVertexIndex
    {
        get
        {
            return _startVertexIndex;
        }
    }

    private int _endVertexIndex = 0;
    /// <summary>
    /// 终点索引
    /// </summary>
    public int EndVertexIndex
    {
        get
        {
            return _endVertexIndex;
        }
    }

    private int _vertexCount = 0;
    /// <summary>
    /// 该行占的点数目
    /// </summary>
    public int VertexCount
    {
        get
        {
            return _vertexCount;
        }
    }

    public Line(int startVertexIndex, int length)
    {
        _startVertexIndex = startVertexIndex;
        _endVertexIndex = length * 6 - 1 + startVertexIndex;
        _vertexCount = length * 6;
    }
}


[AddComponentMenu("UI/Effects/TextSpacing")]
public class TextSpacing : BaseMeshEffect
{
    public float _textSpacing = 1f;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive() || vh.currentVertCount == 0)
        {
            return;
        }

        Text text = GetComponent<Text>();
        if (text == null)
        {
            Debug.LogError("Missing Text component");
            return;
        }

        List<UIVertex> vertexs = new List<UIVertex>();
        vh.GetUIVertexStream(vertexs);
        int indexCount = vh.currentIndexCount;

        string[] lineTexts = text.text.Split('\n');

        Line[] lines = new Line[lineTexts.Length];

        //根据lines数组中各个元素的长度计算每一行中第一个点的索引,每个字、字母、空母均占6个点
        for (int i = 0; i < lines.Length; i++)
        {
            //除最后一行外,vertexs对于前面几行都有回车符占了6个点
            if (i == 0)
            {
                lines[i] = new Line(0, lineTexts[i].Length + 1);
            }
            else if (i > 0 && i < lines.Length - 1)
            {
                lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length + 1);
            }
            else
            {
                lines[i] = new Line(lines[i - 1].EndVertexIndex + 1, lineTexts[i].Length);
            }
        }

        UIVertex vt;

        for (int i = 0; i < lines.Length; i++)
        {
            Vector3 startPos = Vector3.zero;
            Vector3 endPos = Vector3.zero;
            Vector3 defaultStartPos = Vector3.zero;
            Vector3 defaultEndPos = Vector3.zero;
            for (int j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
            {
                if (j < 0 || j >= vertexs.Count)
                {
                    continue;
                }
                vt = vertexs[j];
                if (defaultStartPos == Vector3.zero)
                {
                    defaultStartPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
                }
                defaultEndPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
                if (j != 0)
                {
                    vt.position += new Vector3(_textSpacing * ((j - lines[i].StartVertexIndex) / 6), 0, 0);
                }
                if (startPos == Vector3.zero)
                {
                    startPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
                }
                endPos = new Vector3(vt.position.x, vt.position.y, vt.position.z);
                vertexs[j] = vt;
                //以下注意点与索引的对应关系
                if (j % 6 <= 2)
                {
                    vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
                }
                if (j % 6 == 4)
                {
                    vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
                }
            }
            if (text.alignment == TextAnchor.MiddleCenter || text.alignment == TextAnchor.UpperCenter || text.alignment == TextAnchor.LowerCenter)
            {
                Vector3 defaultCenterPos = defaultStartPos + (defaultEndPos - defaultStartPos) / 2;
                Vector3 centerPos = startPos + (endPos - startPos) / 2;
                for (int j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
                {
                    if (j < 0 || j >= vertexs.Count)
                    {
                        continue;
                    }
                    vt = vertexs[j];
                    vt.position = vt.position + defaultCenterPos - centerPos;
                    vertexs[j] = vt;
                    //以下注意点与索引的对应关系
                    if (j % 6 <= 2)
                    {
                        vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
                    }
                    if (j % 6 == 4)
                    {
                        vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
                    }
                }
            }
            if (text.alignment == TextAnchor.MiddleRight || text.alignment == TextAnchor.UpperRight || text.alignment == TextAnchor.LowerRight)
            {
                Vector3 defaultRightPos = defaultEndPos;
                Vector3 rightPos = endPos;
                for (int j = lines[i].StartVertexIndex; j <= lines[i].EndVertexIndex; j++)
                {
                    if (j < 0 || j >= vertexs.Count)
                    {
                        continue;
                    }
                    vt = vertexs[j];
                    vt.position = vt.position + defaultRightPos - rightPos;
                    vertexs[j] = vt;
                    //以下注意点与索引的对应关系
                    if (j % 6 <= 2)
                    {
                        vh.SetUIVertex(vt, (j / 6) * 4 + j % 6);
                    }
                    if (j % 6 == 4)
                    {
                        vh.SetUIVertex(vt, (j / 6) * 4 + j % 6 - 1);
                    }
                }
            }
        }
    }
}

直接放在有text组件的物体上就可以调节了。
本文仅作为个人笔记。

posted @   一世癫狂  阅读(57)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示