Files
plugin-library/Assets/00.StaryEvo/Editor/EditorFont/ChangeFont.cs
2025-03-31 11:16:52 +08:00

47 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
namespace Stary.Evo.Editor
{
public class ChangeFont :IFontChange
{
Font toChange;
static Font toChangeFont;
FontStyle toFontStyle;
static FontStyle toChangeFontStyle;
public void Creat()
{
EditorGUILayout.Space();
toChange = (Font) EditorGUILayout.ObjectField(toChange, typeof(Font), true, GUILayout.MinWidth(100),GUILayout.MinHeight(30));
toChangeFont = toChange;
EditorGUILayout.Space();
toFontStyle = (FontStyle) EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f),GUILayout.MinHeight(30));
toChangeFontStyle = toFontStyle;
EditorGUILayout.Space();
GUI.color = Color.green;
if (GUILayout.Button("更换",GUILayout.MinWidth(200), GUILayout.MinHeight(50)))
{
Change();
}
}
private void Change()
{
var tArray = Resources.FindObjectsOfTypeAll(typeof(Text));
for (int i = 0; i < tArray.Length; i++)
{
Text t = tArray[i] as Text;
//这个很重要博主发现如果没有这个代码unity是不会察觉到编辑器有改动的自然设置完后直接切换场景改变是不被保存的
//如果不加这个代码,在做完更改后,自己随便手动修改下场景里物体的状态,在保存就好了
Undo.RecordObject(t, t.gameObject.name);
t.font = toChangeFont;
t.fontStyle = toChangeFontStyle;
//相当于让他刷新下 不然unity显示界面还不知道自己的东西被换掉了,还会呆呆的显示之前的东西
EditorUtility.SetDirty(t);
}
Debug.Log("Succed");
}
}
}