Skip to content

Commit

Permalink
fix: fonts break on scene change
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Aug 19, 2024
1 parent 20c98c3 commit cec5ae5
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions Editor/UI/Localization/LanguagePrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_2022_1_OR_NEWER
using UnityEngine.TextCore.Text;
#endif
using UnityEngine.UIElements;

#endregion

Expand Down Expand Up @@ -42,6 +42,8 @@ private static void Init()
fontCallback();
}
};

EditorApplication.update += MaybeRefreshActiveFont;
}

private static string GetSystemLanguage()
Expand Down Expand Up @@ -231,18 +233,42 @@ public static void RegisterLanguage(string languageCode)
RegisteredLanguages = RegisteredLanguages.Add(languageCode.ToLowerInvariant());
}

#if UNITY_2022_1_OR_NEWER
private static IDictionary<string, StyleFontDefinition> FontCache =
new Dictionary<string, StyleFontDefinition>();
new Dictionary<string, StyleFontDefinition>();

private static string _activeFontLanguage;
private static StyleFontDefinition _activeFont;

private static bool ActiveFontValid => _activeFont.value.fontAsset != null;

private static StyleFontDefinition ActiveFont
{
get
{
if (ActiveFontValid) return _activeFont;

_activeFontLanguage = Language;
return TryLoadFontForLanguage(Language);
}
}

private static StyleFontDefinition PreferredFont => TryLoadFontForLanguage(Language);
private static void MaybeRefreshActiveFont()
{
// the active font can be invalidated on scene change. unfortunately unity callbacks are unreliable at
// detecting this, so we need to check every frame.
if (ActiveFontValid && _activeFontLanguage == Language) return;

_activeFont = TryLoadFontForLanguage(Language);

foreach (var fontCallback in _fontUpdateCallbacks.Values) fontCallback();
}

private static StyleFontDefinition TryLoadFontForLanguage(string lang)
{
if (FontCache.TryGetValue(lang, out var font)
&& (font.keyword != StyleKeyword.Undefined || font.value.fontAsset != null)) return font;
var definitions = System.IO.File.ReadAllLines("Packages/nadena.dev.ndmf/Editor/UI/Localization/font_preferences.txt");

var definitions = File.ReadAllLines("Packages/nadena.dev.ndmf/Editor/UI/Localization/font_preferences.txt");

FontAsset currentFont = null;
foreach (var line in definitions)
Expand Down Expand Up @@ -320,12 +346,7 @@ private static void DetachFromPanelForFont(DetachFromPanelEvent evt)

private static void UpdateElementFont(VisualElement elem)
{
elem.style.unityFontDefinition = PreferredFont;
}
#else
public static void ApplyFontPreferences(VisualElement elem) {
// Unity 2019 users will need to deal with garbage font rendering, sorry.
elem.style.unityFontDefinition = ActiveFont;
}
#endif
}
}

0 comments on commit cec5ae5

Please sign in to comment.