前一段时间公司要搞字体替换,实在不想做无脑的手动替换工作,就自己写了个替换当前场景下UI字体的小工具,话不多说上硬货。
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.UI;
public class MyText : EditorWindow {
[MenuItem("Tools/替换字体")]
public static void Open()
{
EditorWindow.GetWindow (typeof(MyText));
}
Font change;
static Font changeFont;
Font toChange;
static Font toChangeFont;
void OnGUI()
{
change = (Font)EditorGUILayout.ObjectField ("目标字体",change, typeof(Font), true, GUILayout.MinWidth (100f));
changeFont = change;
toChange = (Font)EditorGUILayout.ObjectField ("更换字体",toChange, typeof(Font), true, GUILayout.MinWidth (100f));
toChangeFont = toChange;
if (GUILayout.Button ("更换"))
{
ChangeText ();
}
}
public static void ChangeText()
{
Transform canvas = GameObject.Find("Canvas").transform;
if (!canvas)
{
Debug.Log("sence no canvas");
return;
}
Transform[] tArray = canvas.GetComponentsInChildren<Transform>();
for (int i = 0; i < tArray.Length; i++)
{
Text t = tArray [i].GetComponent<Text> ();
if (t)
{
Undo.RecordObject (t,t.gameObject.name);
if (t.font == changeFont)
{
t.font = toChangeFont;
Debug.Log ("替换成功" + t.gameObject.name);
EditorUtility.SetDirty (t);
}
}
}
}
}
网友评论