- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
 
- 纳金币
- 52352
- 精华
- 343
|
我们做项目的时候经常会遇到要换个字体的工作情况,比如美工同学觉着字体不好看或者要做其它语言版本什么的。遇到这种情况我们总不能一个标签一个标签的去找到它们把字体换了,累不累就不说了,万一漏了也是麻烦事。
作为一名高贵的游戏开发工程师,怎么能把时间经历都浪费在这些小事上,所以我们要时刻考虑到什么工作可以通过代码让电脑代替自己的工。
下面这个类是个编辑器工具类,都在代码里~- using UnityEngine;
- using System.Collections;
- using UnityEditor;
- using UnityEngine.UI;
-
- public class ChangeFontWindow : EditorWindow
- {
- [MenuItem("DuanTools/换字体")]
- public static void Open()
- {
- EditorWindow.GetWindow(typeof(ChangeFontWindow));
- }
-
- public Font toChange;
- static Font toChangeFont;
-
- void OnGUI()
- {
- toChange = (Font)EditorGUILayout.ObjectField(toChange, typeof(Font), true, GUILayout.MinWidth(100f));
- toChangeFont = toChange;
- if (GUILayout.Button("变变变!"))
- {
- Change();
- }
- }
-
- public static void Change()
- {
- //获取所有UILabel组件
- //如果是UGUI讲UILabel换成Text就可以
- Object[] labels = Selection.GetFiltered(typeof(UILabel), SelectionMode.Deep);
- foreach (Object item in labels)
- {
- //如果是UGUI讲UILabel换成Text就可以
- UILabel label = (UILabel)item;
- label.trueTypeFont = toChangeFont;
- //label.font = toChangeFont;(UGUI)
- Debug.Log(item.name + ":" + label.text);
- }
- }
- }
复制代码
下面是使用方法,首先打开刚才制作的工具窗口,然后选择要穿的新字体。
然后去场景选择一组UI的最父级。
最后点击变变变,大功告成~!
|
|