Skip to content

Instantly share code, notes, and snippets.

@nukadelic
Last active June 6, 2023 15:45
  • Star 37 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Unity Editor Font Size Changer -- EditorStyles

Unity Editor font size editor for the editor styles and skins

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
@Dzynek
Copy link

Dzynek commented May 13, 2020

image
Unity 5.6.7f1 errors

Assets/EditorFontSize.cs(11,24): error CS1644: Feature `expression bodied members' cannot be used because it is not part of the C# 4.0 language specification
Assets/EditorFontSize.cs(29,29): error CS1644: Feature `null propagating operator' cannot be used because it is not part of the C# 4.0 language specification
Assets/EditorFontSize.cs(167,34): error CS1644: Feature `expression bodied members' cannot be used because it is not part of the C# 4.0 language specification
Assets/EditorFontSize.cs(169,39): error CS1644: Feature `expression bodied members' cannot be used because it is not part of the C# 4.0 language specification

@camta005
Copy link

This is fantastic, you saved my marriage!
I notice that it doesn't work for the package manager, any change of adding that?

@camta005
Copy link

@Dzynek you need to put the script in a folder named Editor

@Dzynek
Copy link

Dzynek commented May 21, 2020

Putting it in the Editor directory where I have Unity installed, e.g. "D: \ Unity \ Editors \ 2019.3.12f1 \ Editor" does not work (I tested on Unity from 5.6 to 2019.3)

It works (from Unity 2017 upwards) to be uploaded to the Assets catalog.

@camta005
Copy link

@Dzynek you need to create a folder called Editor in the assets folder of your project and put it in there.

@philippludwig
Copy link

Thank you, this works really well!

@naps62
Copy link

naps62 commented Nov 14, 2020

Can't believe it took almost an hour of googling for this to show up. Thank you 🙏

@zaqxs123456
Copy link

Great tool, will try it on Linux tonight.

@zaqxs123456
Copy link

A workaround for the stupid "Unity editor can't use UI scale on Linux" problem.

@spirifoxy
Copy link

A workaround for the stupid "Unity editor can't use UI scale on Linux" problem.

I also found another solution which seems to help with scaling (tested on Manjaro) - try to launch the editor directly from the console like this:

GDK_SCALE=2 GDK_DPI_SCALE=0.5 /home/your_user/Unity/Hub/Editor/version/Editor/Unity --projectpath /path/to/project

@zaqxs123456
Copy link

@spirifoxy This should work fine on 4k monitor, but on my 2k monitor it make the UI huge, project tab cover half of the screen. I think Unity
still don't support fractional scaling for Linux.

@derwaldgeist
Copy link

This is a life saver. Thank you so much!

@agnar
Copy link

agnar commented Apr 21, 2022

Thank you very much. It works great

@chopin1998
Copy link

it works good,

but once enter play mode, font change to small again..

@nukadelic
Copy link
Author

updated , now you have

public static int DEFAULT_GLOBAL_FONT_SIZE = 14;
public static bool RESIZE_ON_LAUNCH = true; 

sometimes its a bit buggy tho , so just edit to RESIZE_ON_LAUNCH to false , if the fonts go haywire

@mortoray
Copy link

mortoray commented Jan 6, 2023

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

@nukadelic
Copy link
Author

This is a hack for older versions of the editor , pretty sure in the newer versions you can just change the UI Scaling in the Edit > Preferences window

image

@zaqxs123456
Copy link

This is a hack for older versions of the editor , pretty sure in the newer versions you can just change the UI Scaling in the Edit > Preferences window

image

Are you sure it is on Linux? I can't find that setting.

@nukadelic
Copy link
Author

nukadelic commented Jan 14, 2023

no idea ¯\_(ツ)../¯

@mortoray
Copy link

It's not on Linux, unfortunately.

@mavinii
Copy link

mavinii commented Jan 17, 2023

Well, I'm on Fedora, I just installed version 3.3.0 of Unity, which was the one I managed to get working for my Fedora from the store. I do not need a new version of Unity because this is only for my 3D Game Class.

The thing that worked for me was to reduce the resolution of my screen to 2560 x 1440 for 1920 x 1080. It is still small, but it is better now.

I hope Unity fix this ASAP, for those who need it.

@chopin1998
Copy link

seems not work on 2021.3.11f1, window stuck

@blueforesticarus
Copy link

How tf is this not a solved issue?

@derwaldgeist
Copy link

derwaldgeist commented Mar 21, 2023

Unfortunately, this script is rather unreliable. Sometimes, it does not work at all, sometimes it only increases the size of some of the fonts (Unity 2021). Would love to see a native solution for macOS and Linux by the Unity team, but they seem to just ignore the request. It's so hard to use Unity if you don't have perfect eyesight. It's a shame.

@RIvance
Copy link

RIvance commented Mar 22, 2023

There is a compromise solution to make the ui size 2x bigger on linux (works well for me on gnome):
https://www.youtube.com/watch?v=6N5zUS9Qv9E
Screenshot from 2023-03-22 18-41-56

@derwaldgeist
Copy link

I really wish this was possible on macOS, too.

@attishno1
Copy link

This script doesn't seem to work on Arch Linux, even when I put it in the editor folder of my Unity Project.

@nukadelic
Copy link
Author

I don't have arch to try it on , but if you include info about which unity editor version & linux version u r using someone might drop a hint here ?

@mquaker
Copy link

mquaker commented May 4, 2023

Simply adding a script in OSX 2021.3.24f1 (LTS) will increase the font size properly without any warnings or errors.
However, the Editor Font Size window does not open, and there are no errors or warnings displayed for this issue either.

Anyway, I am happy with this result. My eyes are a little more comfortable now.

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