Change Font in Unity3d

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[] { "修改所有的UI预制文件", "指定要修改的对象"};
	string uiprefabsDir = "Assets/Resources/UI";
	GameObject targetPrefab;
    bool NormalFont;

	private void OnGUI()
	{
		EditorGUILayout.LabelField("该工具会将默认的Arial字体换成指定的新字体");

		newFont = EditorGUILayout.ObjectField("new font", newFont, typeof(Font), false) as Font;

        //List<GameObject> targets = new List<GameObject>();
        GUILayout.Space(20);
        NormalFont = GUILayout.Toggle(NormalFont, "字体粗体改为正常");

        GUILayout.Space(20);
		tabIdx = GUILayout.Toolbar(tabIdx, tabNames);
		switch (tabIdx)
		{
			case 0:
				{
					uiprefabsDir = EditorGUILayout.TextField("UI prefabs 目录", uiprefabsDir);
					if (GUILayout.Button("替换"))
					{
						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("需要指定new font");
						}
					}
				}
				break;
			case 1:
				{
					targetPrefab = EditorGUILayout.ObjectField(targetPrefab, typeof(GameObject), true) as GameObject;
					if (GUILayout.Button("替换"))
					{
						if (newFont && targetPrefab)
						{
							DoChange(targetPrefab);
						}
						else if (newFont == null)
						{
							Debug.LogError("需要指定new font");
						}
						else if (targetPrefab == null)
						{
							Debug.LogError("需要指定要修改的对象");
						}
					}
				}
				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)//当前字体微软雅黑,不包含粗体,改成Normal ,大小+2 lihui 2019/3/14
            {
                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);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值