今天突然来个替换字体需求,其实不难,但是有些编写函数忘记了=。=
直接贴代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class FontToolEditor : EditorWindow
{
private const string MENU_MAIN_WINDOW = "Tools/Font替换";
static Font defaultFont_1 = new Font("FZCuYuan");
static Font defaultFont_2 = new Font("Arial");
static Font toFont = AssetDatabase.LoadAssetAtPath<Font>("Assets/Font/UTMCafeta.ttf");
[MenuItem(MENU_MAIN_WINDOW)]
private static void Init()
{
List<GameObject> prefabs = new List<GameObject>();
var resourcesPath = "Assets/Res/Prefab/Gui";
var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);
for (int i = 0; i < absolutePaths.Length; i++)
{
EditorUtility.DisplayProgressBar("字体转换中...", "字体转换中...", (float)i / absolutePaths.Length);
string path = absolutePaths[i].Replace("\\", "/");
GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
//GameObject instance = GameObject.Instantiate(prefab) as GameObject;
//替换font
Text[] labels = prefab.GetComponentsInChildren<Text>(true);
for (int q = 0; q < labels.Length; q++)
{
Debug.Log(path+" && " + labels[q].name);
if (!labels[q].font || labels[q].font.name == "FZCuYuan")
{
int fontSize = labels[q].fontSize;
var fonStyle = labels[q].fontStyle;
labels[q].font = toFont;
labels[q].fontSize = fontSize;
labels[q].fontStyle = fonStyle;
EditorUtility.SetDirty(labels[q]);
}
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
}
}