Unity3D解决Text不清晰问题

系列文章目录

Unity工具



一 、前言

在软件开发过程中,常常会遇到Unity中Text文字不清晰的问题,大多数情况是因为字体太小,造成了在分辨率下的文字的模糊问题,如下图所示:

1-1 Scene视图

在这里插入图片描述
也能看还行

1-2 Game视图

在这里插入图片描述
感觉就有点模糊了


二、解决方案

因为一个一个调整的话比较浪费时间,所以还是写编辑器工具比较快

2-1 增加Text字号大小

但是字号增加之后就会看不见,Text的框框也得增大,布局就不对了,所以调整他的Scale大小

2-2 具体多大

  1. 增加字号大小,为了方便就增加一倍
  2. 调整宽高为一倍大小
  3. 调整缩放为原来的一半

2-3 原来的Text

在这里插入图片描述

2-4 调整后的Text

在这里插入图片描述

2-5 调整完对比

在这里插入图片描述
明显变清晰了

二、编辑器工具

代码如下(示例):

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

public class EditorText : Editor
{
    //设置全是单独的Text是可以的,一旦来个Button里面的text改了有问题
    [MenuItem("Tools/调整全是Text清晰度")]
    public static void UpdateText()
    {
        //寻找Hierarchy面板下所有的Text
        var tArray = Resources.FindObjectsOfTypeAll(typeof(Text));
        for (int i = 0; i < tArray.Length; i++)
        {
            Text t = tArray[i] as Text;
            //记录对象
            Undo.RecordObject(t, t.gameObject.name);
            t.fontSize = t.fontSize * 2;

            t.GetComponent<RectTransform>().sizeDelta = new Vector2(t.GetComponent<RectTransform>().rect.width * 2, t.GetComponent<RectTransform>().rect.height * 2);
            t.GetComponent<RectTransform>().localScale = new Vector3(t.GetComponent<RectTransform>().localScale.x / 2, t.GetComponent<RectTransform>().localScale.y / 2, t.GetComponent<RectTransform>().localScale.z / 2);

            //设置已改变
            EditorUtility.SetDirty(t);
        }
        Debug.Log("完成");
    }
    //设置有单独的Text还有父物体是button的Text
    [MenuItem("Tools/调整Text加Button清晰度")]
    public static void UpdateText_Btn()
    {
        //寻找Hierarchy面板下所有的Text
        var tArray = Resources.FindObjectsOfTypeAll(typeof(Text));
        for (int i = 0; i < tArray.Length; i++)
        {
            Text t = tArray[i] as Text;
            //记录对象
            Undo.RecordObject(t, t.gameObject.name);
            t.fontSize = t.fontSize * 2;
            if (t.transform.parent.GetComponent <Button>())
            {             
                float xx = t.transform.parent.GetComponent<RectTransform>().rect.width;
                float yy = t.transform.parent.GetComponent<RectTransform>().rect.height;
             
                t.GetComponent<RectTransform>().localScale = new Vector3(t.GetComponent<RectTransform>().localScale.x / 2, t.GetComponent<RectTransform>().localScale.y / 2, t.GetComponent<RectTransform>().localScale.z / 2);
                t.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 0.5f);
                t.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 0.5f);                
                t.GetComponent<RectTransform>().sizeDelta = new Vector2(xx*(1/ t.GetComponent<RectTransform>().localScale.x),yy*
                    (1 / t.GetComponent<RectTransform>().localScale.x));
                t.GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
                Debug.Log(xx);
            }
            else
            {
                t.GetComponent<RectTransform>().sizeDelta = new Vector2(t.GetComponent<RectTransform>().rect.width * 2, t.GetComponent<RectTransform>().rect.height * 2);
                t.GetComponent<RectTransform>().localScale = new Vector3(t.GetComponent<RectTransform>().localScale.x / 2, t.GetComponent<RectTransform>().localScale.y / 2, t.GetComponent<RectTransform>().localScale.z / 2);
            }
          
            //设置已改变
            EditorUtility.SetDirty(t);
        }
        Debug.Log("完成");
    }
}

2-1 代码里面有两个方法一个适用于全是text文本的情况

2-2 一个适用于既有text文本还有Button的情况

2-3 如果后面有其他设置的会继续添加


总结

如需其他扩展的话,可以自行扩展,
如果觉得本篇文章有用别忘了点个关注,关注不迷路,持续分享更多Unity干货文章。
你的点赞就是对博主的支持,有问题记得留言评论或私聊哦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心疼你的一切

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值