Unity3D:UGUI TextPro文本竖排显示文字

大腿Plus 2018年11月1日11:43:15Unity3D之项目开发评论4,6071阅读模式

在蛮牛上看到有人需要竖排显示Text里面的文本,但是Text又没有这个功能,所以我就在网上找了找,果然有解决办法,然后我就进行了整理,写了一个TextPro的组件,也就是Text的加强版,可以横竖排切换。实现原理是继承了Text,重写了OnPopulateMesh方法。下面上代码。文章源自大腿Plus-https://www.shijunzh.com/archives/1030

using System;
using System.Collections.Generic;

namespace UnityEngine.UI
{
    /// <summary>
    /// Labels are graphics that display text.
    /// </summary>

    [AddComponentMenu("UI/TextPro", 10)]
    public class TextPro : Text
    {
        public bool m_Virtical = false;
        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 (m_Virtical)
            {
                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);
        }
    }
}
Unity3D:UGUI TextPro文本竖排显示文字Unity3D:UGUI TextPro文本竖排显示文字

可以通过Virtical属性改变Text的竖排显示。文章源自大腿Plus-https://www.shijunzh.com/archives/1030

下载链接: https://pan.baidu.com/s/1vrr-Huel82Y1P59ipj5peg 提取码: fddg文章源自大腿Plus-https://www.shijunzh.com/archives/1030

大腿Plus
  • 本文由 发表于 2018年11月1日11:43:15
  • 转载请务必保留本文链接:https://www.shijunzh.com/archives/1030

发表评论