Unity 倒计时字体位置保持不动

  1. 问题描述:倒计时使用系统默认字体,左对齐中。在0到1的时候1字体的三角面窄会向左侧的0靠近,影响用户体验。最好是能固定显示,而不是看起来向数字跳动一样。

  1. 解决方式:---美术画一套字体

---使用5个GameObject。

---代码控制-增加一点点性能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[AddComponentMenu("UI/Effects/TextSetting")]
public class TestSetting : BaseMeshEffect
{
    private List<Vector3> list02;
    protected override void OnEnable()
    {
        list02 = new List<Vector3>();
    }

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive()||vh.currentIndexCount==0)
        {
            return;
        }
        #if UNITY_EDITOR
            Text tt= GetComponent<Text>();
            Debug.Log("text name:" + tt.name);
            Debug.Log("text txt:"+tt.text+" length->"+tt.text.Length);
        #endif
        List<UIVertex> vertexs = new List<UIVertex>();
        vh.GetUIVertexStream(vertexs);
        int indexCount = vh.currentIndexCount;
        UIVertex vt;
        UIVertex vt0;
        UIVertex vt1;
        Vector3 offsetCenter;
        if (list02.Count == 0)
        {
            for (int i = 0; i < indexCount; i++)
            {
                if (i % 3 == 0 && i % 6 == 0)
                {
                    vt0 = vertexs[i];
                    vt1 = vertexs[i + 2];
                    offsetCenter = new Vector3((vt0.position.x + vt1.position.x) * 0.5f, 0, 0);
                    list02.Add(offsetCenter);
                }
            }
        }

        List<Vector3> list01 = new List<Vector3>();
        int j;
        for (int i = 0; i < indexCount; i++)
        {
            vt = vertexs[i];
            j = i / 6;
            if (i % 3 == 0 && i % 6 == 0)
            {
                vt0 = vertexs[i];
                vt1 = vertexs[i + 2];
                offsetCenter = new Vector3((vt0.position.x + vt1.position.x) * 0.5f, 0, 0);
                list01.Add(offsetCenter);
            }
            Vector3 poffset = list02[j] - list01[j];
            vt.position += poffset;
            vertexs[i] = vt;
            if (i % 6 <= 2)
            {
                vh.SetUIVertex(vt, (i / 6) * 4 + i % 6);
            }
            if (i % 6 == 4)
            {
                vh.SetUIVertex(vt, (i / 6) * 4 + i % 6 - 1);
            }
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值