栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity竖排文字的实现(2019以上新版本可用)

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity竖排文字的实现(2019以上新版本可用)

竖排文字网络上主要的实现方式有两种

第一种是对数据进行处理,横转竖排,空格占位。

第二种是重新绘制mesh,重写ModifyMesh方法。

为了一步到位,选择了第二种,但是之前网上的方法,在2019版本上用是报错的,不能支持换行等。这是因为新版unity对text的顶点数管理发生了升级。没办法,只好自己搞一个。顺便支持一下简单的html格式,比如变换颜色,换行,换行变颜色等一些需求,毕竟项目中都要用。

下面这个是老版的报错信息,

 ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

顶点数对不上啦。

不多说了,贴一些代码

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

        List verts = new List();
        helper.GetUIVertexStream(verts);

        Text text = GetComponent();
        string realText = GetStringNoHtml(text.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 lines = new List();
        tg.GetLines(lines);
        int needDelNum = 0;
        for (int i = 0; i < lines.Count; i++)
        {
            UILineInfo line = lines[i];

            int step = i;
            int current = 0;
            int endCharIdx = (i + 1 == lines.Count) ? tg.characterCountVisible : lines[i + 1].startCharIdx;
            for (int j = line.startCharIdx; j < endCharIdx; j++)
            {
                bool isMatch = Regex.IsMatch(text.text[j].ToString(), strRegex);
                bool isShow = (realText.Length > j - needDelNum) &&
                        text.text[j].ToString() == realText[j - needDelNum].ToString();
                if (!isMatch && isShow)
                {
                    modifyText(helper, j - needDelNum, current++, step);
                }
                else
                {
                    needDelNum++;
                    if (isMatch)
                    {
                        break;
                    }
                }

            }
        }
    }

效果图如下:

 

 

整个可运行的工程,请移步下面的链接。我打成了一个包直接传了。

Unity竖排文字的实现(2019以上新版本可用)-C#文档类资源-CSDN下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号