Unity UGUI Text竖排显示

在开发项目中偶尔会用到竖排显示字体,因为原生的UGUI没有这个功能,自己就结合一些网上资料整理写一个UI组件TextVirtical。
在Canvas下点击右键UI里找到TextVirtical创建即可

核心代码:

[AddComponentMenu("UI/TextVirtical", 10)]
public class TextVirtical : Text
{
    public bool IsVirtical = true;
    private float lineSpace = 1;
    private float textSpace = 1;
    private float xOffset = 0;
    private float yOffset = 0;
    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        base.OnPopulateMesh(toFill);
        if (IsVirtical)
        {
            VirticalText(toFill);
        }
    }

    private void VirticalText(VertexHelper toFill)
    {
        if (!IsActive())
            return;

        lineSpace = fontSize * lineSpacing;
        textSpace = fontSize * lineSpacing;

        xOffset = rectTransform.sizeDelta.x / 2 - fontSize / 2;
        yOffset = rectTransform.sizeDelta.y / 2 - fontSize / 2;

        for (int i = 0; i < cachedTextGenerator.lines.Count; i++)
        {
            UILineInfo line = cachedTextGenerator.lines[i];

            int step = i;
            if (i + 1 < cachedTextGenerator.lines.Count)
            {
                UILineInfo line2 = cachedTextGenerator.lines[i + 1];

                int current = 0;

                for (int j = line.startCharIdx; j < line2.startCharIdx - 1; j++)
                {
                    modifyText(toFill, j, current++, step);
                }
            }
            else if (i + 1 == cachedTextGenerator.lines.Count)
            {
                int current = 0;
                for (int j = line.startCharIdx; j < cachedTextGenerator.characterCountVisible; j++)
                {
                    modifyText(toFill, j, current++, step);
                }
            }
        }
    }

    void modifyText(VertexHelper helper, int i, int charYPos, int charXPos)
    {
        UIVertex lb = new UIVertex();
        helper.PopulateUIVertex(ref lb, i * 4);

        UIVertex lt = new UIVertex();
        helper.PopulateUIVertex(ref lt, i * 4 + 1);

        UIVertex rt = new UIVertex();
        helper.PopulateUIVertex(ref rt, i * 4 + 2);

        UIVertex rb = new UIVertex();
        helper.PopulateUIVertex(ref rb, i * 4 + 3);

        Vector3 center = Vector3.Lerp(lb.position, rt.position, 0.5f);
        Matrix4x4 move = Matrix4x4.TRS(-center, Quaternion.identity, Vector3.one);

        float x = -charXPos * lineSpace + xOffset;
        float y = -charYPos * textSpace + yOffset;

        Vector3 pos = new Vector3(x, y, 0);
        Matrix4x4 place = Matrix4x4.TRS(pos, Quaternion.identity, Vector3.one);
        Matrix4x4 transform = place * move;

        lb.position = transform.MultiplyPoint(lb.position);
        lt.position = transform.MultiplyPoint(lt.position);
        rt.position = transform.MultiplyPoint(rt.position);
        rb.position = transform.MultiplyPoint(rb.position);

        helper.SetUIVertex(lb, i * 4);
        helper.SetUIVertex(lt, i * 4 + 1);
        helper.SetUIVertex(rt, i * 4 + 2);
        helper.SetUIVertex(rb, i * 4 + 3);
    }
}

}`

自己整理一个package地址:链接:https://pan.baidu.com/s/1YDf3ireTh2n3ZVf4Js581A 提取码:p7sz
使用方式: 在导入package后在Canvas下点击鼠标右键UI里找到TextVirtical创建即可

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值