using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.UI;
public class ChangeFontEditor : EditorWindow {
[MenuItem("Tools/Change Font")]
static void CreateWindow()
{
GetWindow<ChangeFontEditor>("Change Font");
}
Font newFont;
int tabIdx;
string[] tabNames = new string[] { "Modify all UI prefabs", "Specify the objects to be modified"};
string uiprefabsDir = "Assets/Resources/UI";
GameObject targetPrefab;
bool NormalFont;
private void OnGUI()
{
EditorGUILayout.LabelField("The tool will replace the default Arial font with the specified new font");
newFont = EditorGUILayout.ObjectField("new font", newFont, typeof(Font), false) as Font;
//List<GameObject> targets = new List<GameObject>();
GUILayout.Space(20);
NormalFont = GUILayout.Toggle(NormalFont, "Bold font changed to normal");
GUILayout.Space(20);
tabIdx = GUILayout.Toolbar(tabIdx, tabNames);
switch (tabIdx)
{
case 0:
{
uiprefabsDir = EditorGUILayout.TextField("UI prefabs Catalog", uiprefabsDir);
if (GUILayout.Button("replace"))
{
if (newFont)
{
string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { uiprefabsDir });
foreach (string guid in guids)
{
string filepath = AssetDatabase.GUIDToAssetPath(guid);
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(filepath);
DoChange(go);
}
}
else
{
Debug.LogError("Need to specify new font");
}
}
}
break;
case 1:
{
targetPrefab = EditorGUILayout.ObjectField(targetPrefab, typeof(GameObject), true) as GameObject;
if (GUILayout.Button("repolace"))
{
if (newFont && targetPrefab)
{
DoChange(targetPrefab);
}
else if (newFont == null)
{
Debug.LogError("Need to specify new font");
}
else if (targetPrefab == null)
{
Debug.LogError("Need to specify the object to be modified");
}
}
}
break;
}
}
void DoChange(GameObject target)
{
Text[] texts = target.GetComponentsInChildren<Text>(true);
bool changed = false;
bool boldchanged = false;
int cnt = 0;
int boldCnt = 0;
foreach (Text t in texts)
{
if (t.font && t.font.name == "Arial")
{
t.font = newFont;
changed = true;
cnt++;
}
if (NormalFont && t.fontStyle == FontStyle.Bold)//The current font Microsoft elegant black, does not contain bold, change to Normal , size +2
{
t.fontStyle = FontStyle.Normal;
t.fontSize += 2;
boldchanged = true;
boldCnt++;
}
}
if (changed)
{
EditorUtility.SetDirty(target);
Debug.LogFormat("fontChanged - {0} - {1}", target.name, cnt);
}
if (boldchanged)
{
EditorUtility.SetDirty(target);
Debug.LogFormat("boldChanged - {0} - {1}", target.name, boldCnt);
}
}
}