Unity 替换所有字体工具(包括Prefab)
标签:obj oid sys get ase edit resources uid code
1.方便快捷替换所有text字体
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
///
/// 需要指定字体的路径 名字 路径放到Resources文件夹中
///
public class MyTool : EditorWindow
{
//替换场景内的所有字体
[MenuItem("FontTools/替换场景中所有text字体")]
public static void ChangeFont_Scene()
{
//加载目标字体 "目标字体的名字"
Font targetFont = Resources.Load("简启体");
//获取场景所有激活物体
//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 )
{
//带有Text组件的GameObject,替换字体
tmpText = allObj[i].GetComponent();
if (tmpText != null)
{
textCount++;
tmpText.font = targetFont;
//在此扩展,可以给添加外边框,也可以根据需求进行其他操作
//allObj[i].AddComponent();
}
}
Debug.Log(" 当前场景共有:物体 " + allObj.Length + " 个,Text组件 " + textCount + " 个 ");
}
//替换资源文件夹中全部Prefab的字体
[MenuItem("FontTools/替换预设物中所有text字体")]
public static void ChangeFont_Prefab()
{
Font targetFont = Resources.Load("简启体");
List textList = new List();
//获取Asset文件夹下所有Prefab的GUID
string[] ids = AssetDatabase.FindAssets("t:Prefab");
string tmpPath;
GameObject tmpObj;
Text[] tmpArr;
for (int i = 0; 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();
if (tmpArr != null && tmpArr.Length > 0)
textList.Add(tmpArr);
}
}
}
//替换所有Text组件的字体
int textCount = 0;
for (int i = 0; i )
{
for (int j = 0; j )
{
textCount++;
textList[i][j].font = targetFont;
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log(" 当前ProJect共有:Prefab " + ids.Length + " 个,带有Text组件Prefab " + textList.Count + " 个,Text组件 " + textCount + " 个 ");
}
}
#endif
因为不想一个一个替换所以记录一下,方便使用
Unity 替换所有字体工具(包括Prefab)
标签:obj oid sys get ase edit resources uid code
原文地址:https://www.cnblogs.com/qq2351194611/p/14591920.html
评论