Unity编辑器拓展之十四:字体替换工具

博客迁移

个人博客站点,欢迎访问,www.jiingfengji.tech

字体替换工具

这里写图片描述

选择NGUI还是UGUI,选择替换的字体以及字号比例,就可以替换工程目录下所有预制体的字体了

脚本

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);
			}
		}
	}
}

代码下载

https://github.com/JingFengJi/ReplaceFont.git

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Unity编辑器拓展Editor Extension)可以通过自定义的脚本来扩展Unity编辑器的功能和界面,以满足特定项目的需求。通过编辑器拓展,开发者可以创建自定义的编辑器窗口、工具栏按钮、菜单项、检视面板等,来增强Unity编辑器的功能和流程。 要创建一个Unity编辑器拓展,你可以使用C#编写一个继承自Editor类的脚本。这个脚本可以通过Unity的Inspector面板来设置相关的属性和行为。以下是一个简单的示例: ```csharp using UnityEngine; using UnityEditor; public class MyEditorExtension : EditorWindow { [MenuItem("Custom Tools/My Editor Window")] public static void OpenWindow() { // 创建并打开一个自定义的编辑器窗口 MyEditorExtension window = (MyEditorExtension)EditorWindow.GetWindow(typeof(MyEditorExtension)); window.Show(); } private void OnGUI() { // 在编辑器窗口中绘制UI元素 GUILayout.Label("Hello, I am a custom editor window!"); if (GUILayout.Button("Click Me")) { Debug.Log("Button clicked!"); } } } ``` 上述代码创建了一个自定义的编辑器窗口,并在窗口中绘制了一个标签和一个按钮。通过在Unity编辑器中点击"Custom Tools"菜单下的"My Editor Window",可以打开这个自定义的编辑器窗口。 除了编辑器窗口,你还可以通过继承Editor类来创建自定义的检视面板、菜单项等。Unity官方文档中有更详细的教程和示例,可以帮助你更深入地了解和使用Unity编辑器拓展

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值