Una pequeña herramienta para cambiar fuentes por lotes, incluidas prefabricaciones y cambios por lotes de fuentes de texto en cada escena.
using System;
using UnityEditor;
using UnityEditor.iOS;
using UnityEditor.SceneManagement;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.UI;
public class ChangeAllFonts : EditorWindow
{
[MenuItem("Tools/ChangeAllFonts")]
public static void Open()
{
EditorWindow.GetWindow(typeof(ChangeAllFonts), true);
}
private static Font _toChangeFont;
private void OnGUI()
{
_toChangeFont = EditorGUILayout.ObjectField("选择需要更换的字体", _toChangeFont, typeof(Font)) as Font;
if (GUILayout.Button("确定更改"))
{
Change();
}
}
private void Change()
{
//改变所有Prefab中的字体
// var GUIDList = AssetDatabase.FindAssets("t:Prefab");
// foreach (var guid in GUIDList)
// {
// var path = AssetDatabase.GUIDToAssetPath(guid);
// var gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
// var textList = gameObject.GetComponentsInChildren<Text>();
// //Debug.Log($"{gameObject.name} {textList.Length}");
// foreach (var text in textList)
// {
// text.font = _toChangeFont;
// text.fontStyle = FontStyle.Bold;
// }
//
// EditorUtility.SetDirty(gameObject);
// PrefabUtility.SavePrefabAsset(gameObject);
// SaveAndRefresh();
// }
// 遍历build setting中的场景,改变场景中的字体及场景中使用的prefab中的字体
foreach (UnityEditor.EditorBuildSettingsScene scene in UnityEditor.EditorBuildSettings.scenes)
{
//在built setting中被勾选
//if (S.enabled)
//{
//}
//得到场景的名称
string name = scene.path;
//打开这个场景
EditorSceneManager.OpenScene(name);
// 遍历场景中的GameObject
foreach (Text obj in FindObjectsOfType<Text>(true))
{
if(PrefabUtility.IsPartOfAnyPrefab(obj.gameObject))
{
Debug.Log($"InPrefab sceneName: {
name} objName: {
obj.name}");
//改变该text对应prefab中text的字体
Text objSource = PrefabUtility.GetCorrespondingObjectFromSource(obj.gameObject)
.GetComponent<Text>();
objSource.font = _toChangeFont;
EditorUtility.SetDirty(objSource);
}
//改变Scene里text的字体
//Debug.Log($"NotInPrefab sceneName: {name} objName: {obj.name}");
obj.font = _toChangeFont;
EditorUtility.SetDirty(obj.gameObject);
}
EditorSceneManager.SaveOpenScenes();
SaveAndRefresh();
};
}
private void SaveAndRefresh()
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}