Forum rules - please read before posting.

Multiple Fonts to use Korean and Japanese

edited August 2021 in Technical Q&A

Hello!

I would like to use one font for everything (Menu, hotspots, subtitles, etc ) except when I change my language to Korean or Japanese, then point have everything ( Menu, hotspots, subtitles, etc ) use that font? Is there a way to do it in code maybe?

Thanks.

P.S.
Sorry, should have been in different category.

Comments

  • (Moved to Technical Q&A)

    You can hook into the OnChangeLanguage event to update your Menus whenever the language has been changed.

    This is most easily done when using Unity UI for your menus, since you can just go through all of the Text components present in your Canvas hieararchy. See this wiki page for a sample script:

    https://adventure-creator.fandom.com/wiki/Per-language_Menus

    AC menus would be a bit more complicated, since you'd have to iterate through each of a Menu's Elements and update their font property in turn. I'd recommend the use of Unity UI in any case, though, since it's better equipped to deal with dynamic resizing based on changing fonts.

  • That's what I was thinking of. I'm currently using AC menus, but I'm not sure how to get the list Menu items, then I would need to loop through all of the MenuElements of each Menu item to update the font.

    Where do I look for that?

  • An AC menu equivalent would be along the lines of:

    using UnityEngine;
    using AC;
    
    public class DynamicFontLanguage : MonoBehaviour
    {
    
        [SerializeField] private Font defaultFont;
        [SerializeField] private PerLanguageFont[] perLanguageFonts = new PerLanguageFont[0];
    
        private void OnEnable ()
        {
            EventManager.OnChangeLanguage += OnChangeLanguage;
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
        }
    
        private void OnDisable ()
        {
            EventManager.OnChangeLanguage -= OnChangeLanguage;
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
        }
    
        private void OnMenuTurnOn (Menu menu, bool isInstant)
        {
            Font newFont = GetFont (Options.GetLanguage ());
            UpdateMenu (menu, newFont);
        }
    
        private void OnChangeLanguage (int languageIndex)
        {
            Font newFont = GetFont (languageIndex);
            foreach (Menu menu in PlayerMenus.GetMenus ())
            {
                UpdateMenu (menu, newFont);
            }
        }
    
        private void GetFont (int languageIndex)
        {
            foreach (PerLanguageFont perLanguageFont in perLanguageFonts)
            {
                if (languageIndex == perLanguageFont.languageIndex && perLanguageFont.font)
                {
                    return perLanguageFont.font;
                }
            }
            return defaultFont;
        }
    
        private void UpdateMenu (Menu menu, Font font)
        {
            foreach (MenuElement element in menu.elements)
            {
                element.font = font;
            }
        }
    
    
        [System.Serializable]
        private class PerLanguageFont
        {
    
            public int languageIndex;
            public Font font;
    
        }
    
    }
    
  • I'm assuming I would just create a GameObject and attach this to it? Seems pretty straight forward.

    This is what I was looking for "PlayerMenus.GetMenus ()"

  • Yes, so long as it's present in the Hierarchy, it should work.

  • You were close.

    Change the private void GetFont to private Font GetFont

    private Font GetFont(int languageIndex)
    {
        foreach (PerLanguageFont perLanguageFont in perLanguageFonts)
        {
            if (languageIndex == perLanguageFont.languageIndex && perLanguageFont.font)
            {
                return perLanguageFont.font;
            }
        }
        return defaultFont;
    }
    
  • Quite right.

  • If anyone finds this, and is looking for a solution that supports TextMeshPro, I have added a script to the same AC Wiki page. It's the second script and works exactly like the first script. It just uses TMP instead:

    https://adventure-creator.fandom.com/wiki/Per-language_Menus

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.