Skip to content

Commit

Permalink
fix: JP fonts disappear after leaving play mode (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore committed Mar 16, 2024
1 parent bfdfc68 commit 3d9f35d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Fixed
- JP fonts disappear after leaving play mode (#199)

### Changed

Expand Down
18 changes: 16 additions & 2 deletions Editor/UI/Localization/LanguagePrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public static class LanguagePrefs
[InitializeOnLoadMethod]
private static void Init()
{
// Exiting playmode destroys the dynamic font assets we create, so we need to recreate and reapply them.
Language = EditorPrefs.GetString(EditorPrefKey, "en-US");
EditorApplication.playModeStateChanged += evt =>
{
foreach (var fontCallback in _fontUpdateCallbacks.Values)
{
fontCallback();
}
};
}

/// <summary>
Expand Down Expand Up @@ -210,7 +218,8 @@ public static void RegisterLanguage(string languageCode)

private static StyleFontDefinition TryLoadFontForLanguage(string lang)
{
if (FontCache.TryGetValue(lang, out var font)) return font;
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");

Expand All @@ -235,7 +244,7 @@ private static StyleFontDefinition TryLoadFontForLanguage(string lang)
currentFont.fallbackFontAssetTable.Add(loadedFont);
}
}

if (currentFont == null)
{
font = new StyleFontDefinition(StyleKeyword.Null);
Expand All @@ -262,6 +271,11 @@ public static void ApplyFontPreferences(VisualElement elem)

elem.RegisterCallback<AttachToPanelEvent>(AttachToPanelForFont);
elem.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelForFont);

if (elem.parent != null)
{
_fontUpdateCallbacks[elem] = () => UpdateElementFont(elem);
}

UpdateElementFont(elem);
}
Expand Down

0 comments on commit 3d9f35d

Please sign in to comment.