在游戏中要逐一修改字体比较繁琐,而且容易遗漏,为了方便修改字体写了两个方法,这里只是替换字体,在此基础上可以给字体添加外边框等,如有需要可以继续扩充···
此类是对字体的操作修改,也可以根据需求,对其他组件或功能进行操作,不仅限于Text~~
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class MyTool
{
//替换场景内的所有字体
[MenuItem("MyTools/ChangeAllFontOfScene")]
public static void ChangeAllFontOfScene()
{
//加载目标字体
Font targetFont = Resources.Load<Font>("Lato");
//获取场景所有激活物体
//GameObject[] objs = FindObjectsOfType(typeof(GameObject)) as GameObject[];
//获取场景所有物体
GameObject[] allObj = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
Text tmpText;
int textCount = 0;
for (int i = 0; i < allObj.Length; i++)
{
//带有Text组件的GameObject,替换字体
tmpText = allObj[i].GetComponent<Text>();
if (tmpText != null)
{
textCount++;
tmpText.font = targetFont;
//在此扩展,可以给添加外边框,也可以根据需求进行其他操作
allObj[i].AddComponent<Outline>();
}
}
Debug.Log("<color=green> 当前场景共有:物体 </color>" + allObj.Length + "<color=green> 个,Text组件 </color>" + textCount + "<color=green> 个 </color>");
}
//替换资源文件夹中全部Prefab的字体
[MenuItem("MyTools/ChangeAllFontOfPrefab")]
public static void ChangeAllFontOfPrefab()
{
Font targetFont = Resources.Load<Font>("Lato");
List<Text[]> textList = new List<Text[]>();
//获取Asset文件夹下所有Prefab的GUID
string[] ids = AssetDatabase.FindAssets("t:Prefab");
string tmpPath;
GameObject tmpObj;
Text[] tmpArr;
for (int i = 0; i < ids.Length; i++)
{
tmpObj = null;
tmpArr = null;
//根据GUID获取路径
tmpPath = AssetDatabase.GUIDToAssetPath(ids[i]);
if (!string.IsNullOrEmpty(tmpPath))
{
//根据路径获取Prefab(GameObject)
tmpObj = AssetDatabase.LoadAssetAtPath(tmpPath, typeof(GameObject)) as GameObject;
if (tmpObj != null)
{
//获取Prefab及其子物体孙物体···的所有Text组件
tmpArr = tmpObj.GetComponentsInChildren<Text>();
if (tmpArr != null && tmpArr.Length > 0)
textList.Add(tmpArr);
}
}
}
//替换所有Text组件的字体
int textCount = 0;
for (int i = 0; i < textList.Count; i++)
{
for (int j = 0; j < textList[i].Length; j++)
{
textCount++;
textList[i][j].font = targetFont;
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("<color=green> 当前ProJect共有:Prefab </color>" + ids.Length + "<color=green> 个,带有Text组件Prefab </color>" + textList.Count + "<color=green> 个,Text组件 </color>" + textCount + "<color=green> 个 </color>");
}
}
#endif