Skip to content

Instantly share code, notes, and snippets.

@r618
Forked from CapnRat/FontSwitcher.cs
Last active November 2, 2023 19:54
Show Gist options
  • Save r618/0fe5e0f81a426cd05859dea778735471 to your computer and use it in GitHub Desktop.
Save r618/0fe5e0f81a426cd05859dea778735471 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);
// replace also skin's font name - the change _sems_ to be ignored/inconsistent with UI otherwise
ReplaceFont(GUI.skin.font, newFontName);
typeof(EditorApplication).GetMethod("RequestRepaintAllViews", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, null);
// after all the above the change seems to be still ignored - manual Reimport of some (any) asset in the project seems to work though
// instead of reimporting some asset, reimport 'Assets/Editor' folder asset (w/o its content..); 'Assets/Editor' porbably exists since we're running in Editor script
// TODO: don't hardcode 'Assets/Editor'
AssetDatabase.ImportAsset("Assets/Editor", ImportAssetOptions.ForceUpdate);
}
}
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment