using System;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using XCharts.Runtime;
using Object = UnityEngine.Object;
//※※※一键替换会遍历所有预制体更换字体,建议只用来更换默认字体使用※※※
//※※※一键替换会遍历所有预制体更换字体,建议只用来更换默认字体使用※※※
//※※※一键替换会遍历所有预制体更换字体,建议只用来更换默认字体使用※※※
public class ReplaceFont : EditorWindow
{
private static ReplaceFont window = null;
private static List<string> prefafbPathList = new List<string>();
private static Font targetFont; //
private static Font curFont; //空置的话就是把空的字体填上目标字体
private static TMP_FontAsset targetProFont;
private static TMP_FontAsset curProFont; //空置的话就是把空的字体填上目标字体
private static float fontSizeRatio = 1f;
private static bool isUGUI = true;
private static GameObject gameObj;
[MenuItem("Tools/替换字体")]
public static void CSVCode()
{
if (window == null)
window = GetWindow(typeof(ReplaceFont)) as ReplaceFont;
GetFiles(new DirectoryInfo(Application.dataPath), "*.prefab", ref prefafbPathList);
window.titleContent = new GUIContent("ReplaceFont");
window.Show();
}
public static void GetFiles(DirectoryInfo directory, string pattern, ref List<string> fileList)
{
if (directory != null && directory.Exists && !string.IsNullOrEmpty(pattern))
{
try
{
foreach (FileInfo info in directory.GetFiles(pattern))
{
string path = info.FullName.ToString();
fileList.Add(path.Substring(path.IndexOf("Assets")));
}
}
catch (Exception)
{
throw;
}
foreach (DirectoryInfo info in directory.GetDirectories())
{
GetFiles(info, pattern, ref fileList);
}
}
}
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
//isUGUI = EditorGUILayout.Toggle("UGUI", isUGUI);
//isUGUI = EditorGUILayout.Toggle("NGUI", !isUGUI);
EditorGUILayout.EndHorizontal();
curFont = (Font)EditorGUILayout.ObjectField("被替换字体(可以空)", curFont, typeof(Font), true);
curProFont =
(TMP_FontAsset)EditorGUILayout.ObjectField("被替换Pro字体(可以空)", curProFont, typeof(TMP_FontAsset), true);
targetFont = (Font)EditorGUILayout.ObjectField("目标字体", targetFont, typeof(Font), true);
targetProFont =
(TMP_FontAsset)EditorGUILayout.ObjectField("目标Pro字体", targetProFont, typeof(TMP_FontAsset), true);
EditorGUILayout.BeginHorizontal();
//EditorGUILayout.LabelField("字号比例:");
//fontSizeRatio = EditorGUILayout.FloatField(fontSizeRatio);
gameObj = (GameObject)EditorGUILayout.ObjectField("替换的预制体", gameObj, typeof(GameObject), true);
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("替换当前预设"))
{
Change(gameObj);
AssetDatabase.SaveAssets();
Debug.Log("字体替换完成!!!");
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("一键替换所有!慎用!"))
{
if (gameObj != null)
{
Debug.LogError("已经添加了预设请确认替换字体所有还是替换当前预设字体!");
return;
}
if (EditorUtility.DisplayDialog("再次确定", ("是否确定一次性替换场景中所有预制的字体! 一共" + prefafbPathList.Count + "个预制"), "确定",
"取消"))
{
for (int i = 0; i < prefafbPathList.Count; i++)
{
GameObject gameObj_s = AssetDatabase.LoadAssetAtPath<GameObject>(prefafbPathList[i]);
//EditorUtility.DisplayProgressBar("进度条", "当前进度", prefafbPathList.Count - i / prefafbPathList.Count);
Change(gameObj_s);
}
EditorUtility.ClearProgressBar();
AssetDatabase.SaveAssets();
}
}
EditorGUILayout.EndHorizontal();
}
static void TextComponentChange(GameObject prefab)
{
Component[] labels = null;
if (isUGUI)
{
labels = prefab.GetComponentsInChildren<Text>(true);
}
else
{
//labels = prefab.GetComponentsInChildren<UILabel>(true);//基本用不到了ngui
}
if (null != labels)
{
foreach (Object item in labels)
{
if (isUGUI)
{
Text text = (Text)item;
int newFontSize = (int)(text.fontSize * fontSizeRatio);
if (curFont == null || text.font.name == curFont.name)
{
text.font = targetFont;
text.fontSize = newFontSize;
}
}
else
{
// UILabel label = (UILabel)item;
// int newFontSize = (int)(label.fontSize * fontSizeRatio);
// if (label.trueTypeFont.name == curFont.name)
// {
// label.trueTypeFont = targetFont;
// label.fontSize = newFontSize;
// }
}
EditorUtility.SetDirty(item);
}
}
}
static void TextProComponentChange(GameObject prefab)
{
Component[] labels = null;
labels = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);
if (null != labels)
{
foreach (Object item in labels)
{
TextMeshProUGUI text = (TextMeshProUGUI)item;
int newFontSize = (int)(text.fontSize * fontSizeRatio);
if (curProFont == null || text.font.name == curProFont.name)
{
text.font = targetProFont;
text.fontSize = newFontSize;
}
EditorUtility.SetDirty(item);
}
}
}
static void ChartComponentChange(GameObject prefab)
{
Component[] charts = null;
charts = prefab.GetComponentsInChildren<BaseChart>(true);
foreach (var item in charts)
{
BaseChart chart = (BaseChart)item;
var _xAxis = chart.EnsureChartComponent<XAxis>();
var _yAxis = chart.EnsureChartComponent<YAxis>();
var _legend = chart.EnsureChartComponent<Legend>();
var _title = chart.EnsureChartComponent<Title>();
var _gridCoord = chart.EnsureChartComponent<GridCoord>();
// 设置字体大小
_title.labelStyle.textStyle.fontSize = UIStylePara.CHARTTITLE_FONTSIZE;
_legend.labelStyle.textStyle.fontSize = UIStylePara.LEGEND_FONTSIZE;
_xAxis.axisLabel.textStyle.fontSize = UIStylePara.CHARTAXIS_FONTSIZE;
_yAxis.axisLabel.textStyle.fontSize = UIStylePara.CHARTAXIS_FONTSIZE;
_yAxis.axisName.labelStyle.textStyle.fontSize = UIStylePara.CHARTAXIS_FONTSIZE;
//设置字体
_title.labelStyle.textStyle.font = targetFont;
_legend.labelStyle.textStyle.font = targetFont;
_xAxis.axisLabel.textStyle.font = targetFont;
_yAxis.axisLabel.textStyle.font = targetFont;
_yAxis.axisName.labelStyle.textStyle.font = targetFont;
_xAxis.axisName.labelStyle.textStyle.font = targetFont;
// 设置图表位置
_gridCoord.layoutIndex = -1;
_gridCoord.left = 0.03f;
_gridCoord.right = 0.12f;
_gridCoord.top = 0.16f;
_gridCoord.bottom = 0.08f;
// 设置字体位置角度
_title.location.align = Location.Align.TopLeft;
_title.location.top = 0.03f;
_title.location.left = 0.005f;
_legend.iconType = Legend.Type.Auto;
_legend.itemWidth = 30;
_legend.itemHeight = 40;
_legend.itemGap = 20;
_legend.location.align = Location.Align.TopRight;
_legend.location.top = 0.05f;
_legend.location.right = 0.05f;
_legend.labelStyle.textStyle.color = UIStylePara.WordColor;
_xAxis.axisLabel.rotate = 30;
_xAxis.axisLabel.distance = 40;
_xAxis.axisLabel.textStyle.color = UIStylePara.WordColor;
_xAxis.axisLabel.textLimit.gap = -50;
_yAxis.axisLabel.distance = 10;
_yAxis.axisLabel.textStyle.color = UIStylePara.WordColor;
for (int i = 0; i < chart.series.Count; i++)
{
chart.series[i].itemStyle.color = UIStylePara.BarColor;
chart.series[i].itemStyle.toColor = UIStylePara.BarToColor;
}
// 处理数据组相关的参数
for (int i = 0; i < chart.series.Count; i++)
{
if (chart.series[i].serieType == "Bar") //处理柱状图
{
// 柱体样式
chart.series[i].barWidth = 0.4f;
chart.series[i].barGap = 0.1f;
chart.series[i].barMaxWidth = 50f;
// 柱体上的数值样式和位置
chart.series[i].EnsureComponent<LabelStyle>().textStyle.font = targetFont;
chart.series[i].label.textStyle.fontSize = UIStylePara.CHARTVALUE_FONTSIZE;
chart.series[i].label.textStyle.color = UIStylePara.WordColor;
chart.series[i].label.offset = new Vector3(0, 20, 0);
}
}
}
}
public static void Change(GameObject prefab)
{
if (null != prefab)
{
TextComponentChange(prefab);
ChartComponentChange(prefab);
TextProComponentChange(prefab);
}
else
{
Debug.LogError("预制体不能为空!");
}
}
}
上面的是一键替换字体脚本,之前因为打包webgl使用unity默认字体字体会不显示的问题才加了这个脚本,因为我这边涉及到了图表插件,所以把图表中的字体也加进去了,不需要的可以自行注释掉;