Last active
December 31, 2023 17:48
-
-
Save karljj1/dc70405de1bdc5527b10d93edf91b3ac to your computer and use it in GitHub Desktop.
Localize the default TextMeshPro font.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Reflection; | |
using TMPro; | |
using UnityEngine; | |
using UnityEngine.Localization; | |
/// <summary> | |
/// Sets the default TextMeshPro font to be used. | |
/// </summary> | |
[ExecuteInEditMode] | |
public class DefaultTmpFont : MonoBehaviour | |
{ | |
public LocalizedTmpFont localizedFont = new LocalizedTmpFont(); | |
public bool updateExistingText = true; | |
void OnEnable() => localizedFont.AssetChanged += LocalizedFont_AssetChanged; | |
void OnDisable() => localizedFont.AssetChanged -= LocalizedFont_AssetChanged; | |
void LocalizedFont_AssetChanged(TMP_FontAsset value) | |
{ | |
// Requires TMP 3.2 or greater | |
TMP_Settings.defaultFontAsset = value; | |
// Do we want to update any text that is already displayed with the new font? | |
if (updateExistingText) | |
{ | |
var o = FindObjectsOfType<TextMeshProUGUI>(); | |
foreach (var p in o) | |
{ | |
p.font = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment