Unity可以独立于操作系统更改编辑器用户界面字体的缩放比例,也可以使用脚本对字体进行全局缩放,因此在本文中将向大家展示如何使用软件内置UI缩放和通过脚本的方式放大文字。
使用软件内置UI缩放
1.转到编辑菜单 → 首选项 → UI缩放。

2.在UI缩放选项上禁用或取消选中使用默认桌面设置选项,这会自动切换为自定义缩放值的下拉列表选项。

4.最后,更改Unity用户界面设置下拉选项中选择百分比,例如I3D设置缩放为175%,重启软件。

5.放大后发现界面有一种莫名其妙的压抑,眼睛有点发胀,2K显示器有点不够用了,只是为何菜单还是原来的大小!!好像只能放大菜单之外的字体及用户界面,或许125%会较为合适些吧。

使用脚本自定义字体大小
默认的软件改变用户界面的字体大小感觉有点“鸡肋”,因此去查看了相关解决方案,有幸查阅了一篇亲测有效的内容,该代码支持自定义样式及全局文本缩放比例。
1.首先新建一个名为“EditorFontSize.cs”的脚本文件,然后将下面的完整代码复制到里面并将该文件复制到项目的资产文件夹中,在其中新建一个名为“Editor”的文件夹并将放入其中。
例如:D:\i3DProject\2022.1.0\Assets\Editor
该脚本默认全局大小为14(DEFAULT_GLOBAL_FONT_SIZE = 14;),如果在使用的过程文本显示存在问题,可以试着将其中的RESIZE_ON_LAUNCH更改为false。
2.完整的脚本。
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class EditorFontSize : EditorWindow
{
// enable resize on launch to set a default font size , using this option will disable the ability to have the window accassible
// from the context menu ( Window > Editor Font Size ) - bc this is a hacky way to enforce default global font size on application
// launch , on script assembly reload and on application enter / exit play mode
public static bool RESIZE_ON_LAUNCH = true;
public static int DEFAULT_GLOBAL_FONT_SIZE = 14;
[InitializeOnLoadMethod] static void DefaultSize()
{
if( ! RESIZE_ON_LAUNCH || DEFAULT_GLOBAL_FONT_SIZE <= 10 ) return;
var w = GetWindow<EditorFontSize>();
w.GUICallback = () => {
w.Resize( DEFAULT_GLOBAL_FONT_SIZE - 10 ) ;
w.Close();
};
}
[MenuItem("Window/Editor Font Size")]
static void Open()
{
if( RESIZE_ON_LAUNCH ) return;
GetWindow<EditorFontSize>("Editor Font Size").minSize = new Vector2(180, 30);
}
Dictionary<string, bool> foldouts;
bool Header(string s)
{
if (foldouts == null) foldouts = new Dictionary<string, bool>();
if (!foldouts.ContainsKey(s)) foldouts.Add(s, true);
GUILayout.Space(5);
var foldout = EditorGUILayout.Foldout(!foldouts[s], s, true);
foldouts[s] = !foldout;
return foldout;
}
private void OnDisable()
{
guiSkins = null;
editorStyles = null;
propFontSizevalidity?.Clear();
propFontSizevalidity = null;
}
static List<GUIStyle> styles;
PropertyInfo[] editorStyles;
PropertyInfo[] guiSkins;
void GrabProperties()
{
if (editorStyles == null || editorStyles.Length < 1)
{
var flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty;
editorStyles = typeof(EditorStyles).GetProperties(flags);
}
if (guiSkins == null || guiSkins.Length < 1)
{
guiSkins = GUI.skin.GetType().GetProperties();
}
}
Vector2 scroll;
System.Action GUICallback;
private void OnGUI()
{
rowCount = -1;
GrabProperties();
InitStyles();
var delta = FontSizeRow("Global Zoom", EditorStyles.miniLabel.fontSize.ToString());
if (delta != 0 && EditorStyles.miniLabel.fontSize + delta > 0)
{
Resize( delta );
}
GUILayout.Label("", GUI.skin.horizontalSlider);
using (var scope = new GUILayout.ScrollViewScope(scroll))
{
scroll = scope.scrollPosition;
if (Header("Editor Styles"))
foreach (var x in editorStyles)
ModifyProp(x, null);
if (Header("GUI skins"))
foreach (var x in guiSkins)
ModifyProp(x, GUI.skin);
if (Header("Custom Styles"))
foreach (var x in GUI.skin.customStyles)
{
FontSizeRow(x);
RepaintAll();
}
}
if( GUICallback != null ) GUICallback.Invoke();
}
void Resize( int delta )
{
// Keep watch for duplicates ( Prevent double modifications )
styles = new List<GUIStyle>();
foreach (var x in editorStyles)
{
if (!ValidFontProp(x, null)) continue;
var s = (GUIStyle)x.GetValue(null, null);
if (styles.Contains(s)) continue;
styles.Add(s);
s.fontSize += delta;
}
foreach (var x in guiSkins)
{
if (!ValidFontProp(x, GUI.skin)) continue;
var s = (GUIStyle)x.GetValue(GUI.skin, null);
if (styles.Contains(s)) continue;
styles.Add(s);
s.fontSize += delta;
}
foreach (var x in GUI.skin.customStyles)
{
if (!ValidFontStyle(x) || styles.Contains(x)) continue;
FixZeroSize(x);
styles.Add(x);
x.fontSize += delta;
}
styles.Clear();
RepaintAll();
}
void RepaintAll() { foreach (var w in Resources.FindObjectsOfTypeAll<EditorWindow>()) w.Repaint(); }
Dictionary<PropertyInfo, bool> propFontSizevalidity;
bool ValidFontProp(PropertyInfo x, object item)
{
if (propFontSizevalidity == null) propFontSizevalidity = new Dictionary<PropertyInfo, bool>();
if (propFontSizevalidity.ContainsKey(x)) return propFontSizevalidity[x];
propFontSizevalidity.Add(x, true);
if (string.IsNullOrEmpty(x.Name)) propFontSizevalidity[x] = false;
else if (x.PropertyType != typeof(GUIStyle)) propFontSizevalidity[x] = false;
else if (x.GetValue(item, null) == null) propFontSizevalidity[x] = false;
else if (((GUIStyle)x.GetValue(item, null)).fontSize < 1) propFontSizevalidity[x] = false;
return propFontSizevalidity[x];
}
void ModifyProp(PropertyInfo x, object item)
{
if (!ValidFontProp(x, item)) return;
var style = ((GUIStyle)x.GetValue(item, null));
var val = style.fontSize;
int ret = FontSizeRow(x.Name, val.ToString());
if (ret == 0 || val + ret <= 0) return;
style.fontSize = val + ret;
RepaintAll();
}
GUIStyle evenBG;
GUIStyle oddBG;
void InitStyles()
{
if (evenBG != null) return;
GUIStyle s = "CN EntryBackEven";
evenBG = new GUIStyle(s);
s = "CN EntryBackodd";
oddBG = new GUIStyle(s);
evenBG.contentOffset = oddBG.contentOffset = new Vector2();
evenBG.clipping = oddBG.clipping = TextClipping.Clip;
evenBG.margin = oddBG.margin =
evenBG.padding = oddBG.padding = new RectOffset();
}
int rowCount = 0;
void FixZeroSize(GUIStyle s) => s.fontSize = s.fontSize < 1 ? 11 : s.fontSize;
bool ValidFontStyle(GUIStyle s) => !(s == null || string.IsNullOrEmpty(s.name));
void FontSizeRow(GUIStyle s)
{
if (!ValidFontStyle(s)) return;
FixZeroSize(s);
var x = FontSizeRow(s.name, s.fontSize.ToString());
if (x != 0 && x + s.fontSize > 0) s.fontSize += x;
}
int FontSizeRow(string name, string size)
{
if (string.IsNullOrEmpty(name) || size == "0") return 0;
rowCount++;
var width = GUILayout.MaxWidth(Screen.width);
using (new GUILayout.HorizontalScope(rowCount % 2 == 0 ? evenBG : oddBG, width))
{
GUILayout.Label(name);
GUILayout.FlexibleSpace();
if (GUILayout.Button("-", EditorStyles.miniButtonLeft)) return -1;
using (new EditorGUI.DisabledGroupScope(true))
GUILayout.Label(size, EditorStyles.miniButtonMid, GUILayout.Width(30));
if (GUILayout.Button("+", EditorStyles.miniButtonRight)) return +1;
}
return 0;
}
}
#endif
源码可下载吗