系列文章目录
Unity工具
文章目录
一 、前言
在软件开发过程中,常常会遇到Unity中Text文字不清晰的问题,大多数情况是因为字体太小,造成了在分辨率下的文字的模糊问题,如下图所示:
1-1 Scene视图
也能看还行
1-2 Game视图
感觉就有点模糊了
二、解决方案
因为一个一个调整的话比较浪费时间,所以还是写编辑器工具比较快
2-1 增加Text字号大小
但是字号增加之后就会看不见,Text的框框也得增大,布局就不对了,所以调整他的Scale大小
2-2 具体多大
- 增加字号大小,为了方便就增加一倍
- 调整宽高为一倍大小
- 调整缩放为原来的一半
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干货文章。
你的点赞就是对博主的支持,有问题记得留言评论或私聊哦