Skip to content

Instantly share code, notes, and snippets.

@CapnRat
Last active November 2, 2023 19:58
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save CapnRat/1e0dad23520b00474646b9baf26cab70 to your computer and use it in GitHub Desktop.
Save CapnRat/1e0dad23520b00474646b9baf26cab70 to your computer and use it in GitHub Desktop.
#if UNITY_EDITOR
using System.Reflection;
using UnityEngine;
using UnityEditor;
public class FontSwitcher : EditorWindow
{
[MenuItem("Font/Show Window")]
public static void ShowFontWindow()
{
GetWindow<FontSwitcher>();
}
public void OnGUI ()
{
EditorGUI.BeginChangeCheck();
string newFontName = EditorGUILayout.DelayedTextField("Unity Font", GUI.skin.font.fontNames[0]);
if (EditorGUI.EndChangeCheck())
{
ReplaceFont((Font)EditorGUIUtility.LoadRequired("Fonts/Lucida Grande.ttf"), newFontName);
ReplaceFont((Font)EditorGUIUtility.LoadRequired("Fonts/Lucida Grande Bold.ttf"), newFontName);
ReplaceFont((Font)EditorGUIUtility.LoadRequired("Fonts/Lucida Grande Small.ttf"), newFontName);
ReplaceFont((Font)EditorGUIUtility.LoadRequired("Fonts/Lucida Grande Small Bold.ttf"), newFontName);
ReplaceFont((Font)EditorGUIUtility.LoadRequired("Fonts/Lucida Grande Big.ttf"), newFontName);
typeof(EditorApplication).GetMethod("RequestRepaintAllViews", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, null);
}
}
private void ReplaceFont(Font font, string fontName)
{
if (font.name.Contains("Bold"))
font.fontNames = new string[] { fontName + " Bold" };
else
font.fontNames = new string[] { fontName };
font.hideFlags = HideFlags.HideAndDontSave;
}
}
#endif
@danhanfry
Copy link

danhanfry commented Jan 31, 2020

Hi. I can't get it to work ..

Could you tell me which line is the one that changes the text?

thanks

@Dzynek
Copy link

Dzynek commented Oct 29, 2020

It doesn't work, I tested on Unity 2017.4, 2019.4
I think you need to create a directory "Assets/Editor Default Resources" and put the font there, otherwise error

@r618
Copy link

r618 commented Nov 2, 2023

I added some (two) things which refresh UI + Editor and font seems to be correctly updating in 2019.4.5, yay
I'd open a PR but not sure if gists can be treated like that lol probably not

https://gist.github.com/r618/0fe5e0f81a426cd05859dea778735471

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