Skip to content

Instantly share code, notes, and snippets.

@nukadelic
Last active December 31, 2024 06:58
Show Gist options
  • Save nukadelic/47474c7e5d4ee5909462e3b900f4cb82 to your computer and use it in GitHub Desktop.
Save nukadelic/47474c7e5d4ee5909462e3b900f4cb82 to your computer and use it in GitHub Desktop.
Unity Editor Font Size Changer -- EditorStyles

Unity Editor font size editor for the editor styles and skins

Change the variable RESIZE_ON_LAUNCH to true or false. Enable resize on launch to set a default font size , using this option will disable the ability to have the window accessible from the context menu ( Window > Editor Font Size ) - because 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 )

Preview Added custom styles ( 600+ ) Added Global Zoom ( increment all styles in the same time )

GIF PREVIEW : https://imgur.com/8jsy8sN

#if UNITY_EDITOR
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
@CSEliot
Copy link

CSEliot commented Aug 1, 2024

I tried this script on Ubuntu 18.04 LTS with unity editor 2022.3.14f1, but it still doesn't work. However, I do find a way to address the problem of small font size. go to /usr/share/applications and find unityhub.desktop, edit Exec=env GDK_SCALE=2 GDK_DPI_SCALE=0.5 /path/to/unityhub %U then it works for me. Only need to change env when running unityhub, and is still accessable via desktop icon. Thanks for the posts above that inspired me this solution.

This was the best solution for me, as the text doesn't get cut off. Thanks! (btw my unityhub path was /opt/unityhub/unityhub ... idk if that's normal or not but i didnt change it)

@OliverKreitmeyer
Copy link

if only there was a similar script like this for modern versions. Im using unity 2022.3.49f1 LTS and this script managed to scale some text, but not their whole labels so the text was just cropped halfway. On Fedora 40 with GNOME. Will test out Unity 6 later.

@OliverKreitmeyer
Copy link

Does not work on Unity 6 either.

@nukadelic
Copy link
Author

they been slowly replacing it with the new system , now unity 6 is mostly using UIToolkit instead of IMGUI for all its editor panels , where its using uss files ( unity variant of CSS )

Its possible to change font size through in Window > UI Toolkit > Debugger . a somehow similar to firefox / chrome dev tools.
image

The style files themselves are being loaded in runtime which looks something like this :

internal class PanelSettingsInspector : Editor
        const string k_DefaultStyleSheetPath = "UIPackageResources/StyleSheets/Inspector/PanelSettingsInspector.uss";
        const string k_InspectorVisualTreeAssetPath = "UIPackageResources/UXML/Inspector/PanelSettingsInspector.uxml";

however i didn't find any of these files in the editor installation folder , its probably being uncompressed on application start and stored in RAM. it should be possible to swap the style files manually but then you would need to edit thousands of lines manually.

Alternatively should be possible to scale all of the root panel elements to something like 150% but it would not respect boundaries at all
image

Otherwise should be possible to extend the script i wrote and just scan for all possible TextElement's and increase the font to your desired % ( eg font size 12px , so u would want to x 1.5 which will give u 18px ) - however this cannot grantee that the style would be persistent unless the script will continuously run , scan and update the values on the fly which would be extremely slow and would just make using the application ab absolute nightmare. By persistent i mean that when you selecting other game objects , the inspector would have to destroy the old components and create new once which will use the default non overridden elements.

I would just recommend just lowering your screen resolution at this point. so instead of using it on a 4k screen , drop it to 1080p , or even 720 as long as you get your desired PPI - sadly not enough people are using your exact operating system and i doubt they will fix that anytime soon. If Linux is instrumental to your needs try a different distro , some folks above have got it working on theirs. Or get a specialized machine just for unity if u can afford it.

I will also try a few hacks when i have some spare time , maybe its doable without flooring the performance

@OliverKreitmeyer
Copy link

Thanks for looking into this! I cant really switch to anything so I guess Ill just have to strain my eyes until GNOME starts supporting fractional scaling better and just scale my whole screen when using Unity... or until Unity adds UI scaling to linux.

@ProgrammingLife
Copy link

ProgrammingLife commented Nov 20, 2024

I'm unable to get this to work on Linux with the below version. Nothing happens when choosing Window | Editor Font Size. Editing the default size doesn't change the size on startup.

2022.2.1f1.112.6257 Revision: 2022.2/release 4fead5835099 Built: Thu, 08 Dec 2022 14:15:26 GMT

The same issue here. Arch Linux. I can't even open it. It does nothing when I try to open it.
Unity6.

@devsleeper
Copy link

devsleeper commented Dec 21, 2024

Working M1 Unity 6000.0.28f1 tyvm!

Edit: While it is technically "working" it's buggy in some areas being way too scaled and others ignored - but still it's better than nothing.

@The-Maize
Copy link

Any update to this?
Unity 6 not working at all for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment