Unity 之UGUI Text组件坚排显示(改进)

19 篇文章 0 订阅
10 篇文章 0 订阅
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

[ExecuteInEditMode]
[RequireComponent(typeof(Text))]
public class VirticalText : BaseMeshEffect
{

    [Tooltip("字和字之间的距离")]
    public float spacing = 1;
    private float lineSpacing = 1;
    private float textSpacing = 1;
    private float xOffset = 0;
    private float yOffset = 0;

    public override void ModifyMesh(VertexHelper helper)
    {
        if (!IsActive())
            return;

        List<UIVertex> verts = new List<UIVertex>();
        helper.GetUIVertexStream(verts);

        Text text = GetComponent<Text>();

        TextGenerator tg = text.cachedTextGenerator;

        lineSpacing = text.fontSize * text.lineSpacing;
        textSpacing = text.fontSize * spacing;

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

        List<UILineInfo> lines = new List<UILineInfo>();
        tg.GetLines(lines);

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

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

                int current = 0;

                for (int j = line.startCharIdx; j < line2.startCharIdx - 1; j++)
                {
                    modifyText(helper, j, current++, step, text.text.Length / 2f * text.fontSize, text.text.Length % 2 == 0 ? text.fontSize : 0, text.fontSize);
                }
            }
            else if (i + 1 == lines.Count)
            {
                int current = 0;
                for (int j = line.startCharIdx; j < tg.characterCountVisible; j++)
                {
                    modifyText(helper, j, current++, step,text.text.Length/2f * text.fontSize, text.text.Length % 2==0? text.fontSize :0, text.fontSize);
                }
            }
        }
    }

    void modifyText(VertexHelper helper, int i, int charYPos, int charXPos,float count,int isDouble,float fontSize)
    {
        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 * lineSpacing + xOffset;
        float y = -charYPos * textSpacing + yOffset- isDouble/2;
        Debug.Log(y+":."+count);
        Vector3 pos = new Vector3(0, y+count-(isDouble!=0? 0 : fontSize / 2));
        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);
    }
}

复制了不知道谁的代码进行了改进,现在为居中显示居中对齐模式
更改对齐模式主要由以下代码控制

        float x = -charXPos * lineSpacing + xOffset;
        float y = -charYPos * textSpacing + yOffset- isDouble/2;
        Debug.Log(y+":."+count);
        Vector3 pos = new Vector3(0, y+count-(isDouble!=0? 0 : fontSize / 2));

pos来控制,可视化的配置改进后续会贴出

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值