Forum rules - please read before posting.

[Localization] Switching Subtitles font [Chinese, Jap] at runtime while using Unity uGUI

Hi there,

which events could I hook to anticipate the displaying of a subtitle, so that it shows and uses the appropriate TextMeshPro font, instead of the multi5 one?
I have 3 TMPro fonts:

  • a multi5 one,
  • one for traditional chinese (part of it)
  • one for japanese.

Currently I'm using TMPro "Fallback font" feature, but it's not error prone.

I'd love to be able to set the appropriate font at runtime, without using that system.

Do you have any hint or advice to share, according your experience?

Thank you :)

Comments

  • edited October 2019

    You can hook into the OnChangeLanguage event to update your UIs.

    This script updates UI fonts for regular Unity UI Text components, but it can be adapted to work with TextMeshPro:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class ChangeUIFont : MonoBehaviour
    {
    
        public Font chineseFont;
        public Font japaneseFont;
        public Font defaultFont;
    
        private void OnEnable()
        {
            EventManager.OnChangeLanguage += OnChangeLanguage;
        }
    
        private void OnDisable()
        {
            EventManager.OnChangeLanguage -= OnChangeLanguage;
        }
    
        private void OnChangeLanguage(int languageIndex)
        {
            if (languageIndex == 2)
            {
                SetFont (chineseFont);
            }
            else if (languageIndex == 1)
            {
                SetFont (japaneseFont);
            }
            else
            {
                SetFont (defaultFont);
            }
        }
    
        private void SetFont (Font font)
        {
            AC.Menu[] allMenus = PlayerMenus.GetMenus().ToArray();
            foreach (AC.Menu menu in allMenus)
            {
                if (menu.canvas != null)
                {
                    Debug.Log ("Updating menu " + menu.title);
                    Text[] texts = menu.canvas.GetComponentsInChildren<Text>();
                    if (texts.Length == 0) Debug.Log ("But no Text components found on " + menu.title);
                    else Debug.Log ("Updating " + texts.Length + " Text components on " + menu.title);
                    foreach (Text text in texts)
                    {
                        text.font = font;
                    }
                }
                else
                {
                    Debug.Log ("No canvas on menu " + menu.title);
                }
            }
        }
    
    }
    
  • Thanks Chris!

    This is another snippet to bookmark.

    I sometime get lost in the so many events to hook to :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.