Unity字体替换

14 篇文章 0 订阅
8 篇文章 0 订阅

前面的两个是在网上找的,属于编译器扩展的工具:

1、原文链接(指定文件路径下所有的预制体的字体被替换):Unity编辑器拓展之十四:字体替换工具_静风霁的博客-CSDN博客_unity 字体替换

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEngine.UI;
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 float fontSizeRatio = 1f;
	private static bool isUGUI = true;

	[MenuItem("Tools/替换字体")]
	public static void CSVCode()
	{
		if (window == null)
		window = EditorWindow.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 (System.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);
		targetFont = (Font) EditorGUILayout.ObjectField("目标字体", targetFont, typeof(Font), true);
		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("字号比例:");
		fontSizeRatio = EditorGUILayout.FloatField(fontSizeRatio);
		EditorGUILayout.EndHorizontal();
		if(GUILayout.Button("一键替换"))
		{
			for (int i = 0; i<prefafbPathList.Count; i++)
			{
				GameObject gameObj = AssetDatabase.LoadAssetAtPath<GameObject>(prefafbPathList[i]);
				Change(gameObj);
			}
		AssetDatabase.SaveAssets();
		}
	}

	public static void Change(GameObject prefab)
	{
		if(null != prefab)
		{
            Component[] labels = null;
			if(isUGUI)
			{
                labels = prefab.GetComponentsInChildren<Text>(true);
			}
			else
			{
                //labels = prefab.GetComponentsInChildren<UILabel>(true);
			}
			if(null != labels)
			foreach (Object item in labels)
			{
				if(isUGUI)
				{
                    Text text = (Text)item;
                    int newFontSize = (int)(text.fontSize * fontSizeRatio);
                    if (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);
			}
		}
	}
}

2、原文链接(这里是选中的预制体下所有的字体被替换):Unity一键修改NGUI字体的编辑器脚本_玛~的博客-CSDN博客

using UnityEngine;
using UnityEditor;

/// <summary>
/// 根据鼠标点中的对象批量修改所有UI字体脚本,脚本位于Editor文件夹
/// </summary>
public class ChangeFontWindow : EditorWindow
{
    //是否改变当前字体
    private static bool isChangFont = false;

    //当前字体
    private static Font curFont;

    //是否改变字体类型
    private static bool isChangeStyle = false;

    //字体类型
    private static FontStyle curFontStyle;

    //是否增加字体大小
    private static bool isExpandSize = false;

    //字体大小增加的值
    private static int fontSizeDelta = 0;

    //window菜单下
    [MenuItem("Window/Change Font")]
    private static void ShowWindow()
    {
        ChangeFontWindow cw = GetWindow<ChangeFontWindow>(true, "修改字体");
        //(强迫症,看着舒服)
        cw.minSize = new Vector2(310, 200);
        cw.maxSize = new Vector2(310, 300);
    }

    private void OnGUI()
    {
        //向下空出5个像素
        GUILayout.Space(5);

        //创建是否改变当前字体开关
        isChangFont = EditorGUILayout.Toggle("是否改变当前字体", isChangFont);
        GUILayout.Space(5);

        //如果改变当前字体则创建字体文件选择框
        if(isChangFont)
        {
            curFont = (Font)EditorGUILayout.ObjectField("目标字体", curFont, typeof(Font), true);
            GUILayout.Space(5);
        }

        //创建是否改变字体类型开关
        isChangeStyle = EditorGUILayout.Toggle("是否改变字体类型", isChangeStyle);
        GUILayout.Space(5);

        //如果改变,则创建字体类型的枚举下拉框
        if(isChangeStyle)
        {
            curFontStyle = (FontStyle)EditorGUILayout.EnumPopup("字体类型", curFontStyle);
            GUILayout.Space(5);
        }

        //创建是否增加字体大小的开关
        isExpandSize = EditorGUILayout.Toggle("是否增加字体大小", isExpandSize);
        GUILayout.Space(5);

        //如果增加字体大小则创建增加字体大小值的滑条
        if(isExpandSize)
        {
            fontSizeDelta = (int)EditorGUILayout.Slider("增加字体大小的值", fontSizeDelta, -200, 200);
            GUILayout.Space(5);
        }

        //创建确认按钮
        if(GUILayout.Button("确认修改", GUILayout.Height(30), GUILayout.Width(300)))
        {
            Change();
        }
    }

    public static void Change()
    {
        //如果鼠标没有选中物体则返回
        if(Selection.objects == null || Selection.objects.Length == 0) { return; }

        //获取点中对象(包括子目录)所有UILabel组件
        Object[] labels = Selection.GetFiltered(typeof(UILabel), SelectionMode.Deep);

        //赋值
        foreach(Object item in labels)
        {
            UILabel label = (UILabel)item;

            if(isChangFont) { label.trueTypeFont = curFont; }

            if(isChangeStyle) { label.fontStyle = curFontStyle; }

            if(isExpandSize) { label.fontSize += fontSizeDelta; }

            EditorUtility.SetDirty(item); //重要(有点像应用设置的意思)
        }
    }
}

----------------------------------------------------------------------------------------------------------

我是在NGUI下替换字体,如果使用上述功能替换会修改到预制体。所以建一个空的预制体,挂载上脚本NGUIFont.cs文件。FontType选择Dynamic,TTF Font下是要使用的字体。如图:

 任何时候需要换字体集时,直接替换TTF Font的字体集就可以了。

在NGUI Label上挂载字体集的预制体就可以了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值