Forum rules - please read before posting.

Changing Font at Runtime

Dear Chris and AC followers.
I'm almost ready with ENCODYA (releasing end of January!) and I'm now polishing few things here and there.
99% of it it's done!
I'm also handling localization now and so far I've used fonts that are fine with most of the languages. But the wishlists of ENCODYA are going so well, that I'll have to handle also Cyrillic and most probably Asian ones too (Chinese, Korean, Japanese).
The problem is that my font don't support those, so I need to change it, but I don't want to change them for the whole game, since I'd like to keep the ones I've for the "standard" languages (Italian, English, German, etc.).
So, considering I've used mainly three fonts (one for the subtitles, one for the UI and one for some terminals/computers that the player can use in-game), is it possible to change fonts when the player changes language? Do I have to do that for each Menu "label" I used in my game (it'll be endless) or I can simply tell "replace THIS font with THAT font" via script and it'll automatically find all the UI "text" component using a specific font and changing it?
Is this somehow doable with AC or purely Unity script?
Looking for some ideas! Thanks a lot!

Comments

  • I'm interested in this too.

  • You can hook into the OnChangeLanguage custom event to update your fonts automatically - for example, in a Unity UI-based menu:

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

  • Thanks a lot! I guess I've to change to Unity UI my subtitles then... They are the only ones still using Adventure Creator menu... Or is there a way to change those as well?

  • edited December 2020

    There is - see the Manual's "Menu scripting" chapter for details on how Menu and Element properties can be accessed through script.

    Here's a variant of the wiki script that works for an AC menu named "Subtitles":

    using UnityEngine;
    using AC;
    
    public class DynamicFontLanguage_Subtitles : 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)
        {
            if (_menu.title == "Subtitles")
            {
                OnChangeLanguage (Options.GetLanguage ());
            }
        }
    
        private void OnChangeLanguage (int languageIndex)
        {
            Font newFont = defaultFont;
            foreach (PerLanguageFont perLanguageFont in perLanguageFonts)
            {
                if (languageIndex == perLanguageFont.languageIndex && perLanguageFont.font)
                {
                    newFont = perLanguageFont.font;
                    break;
                }
            }
    
            if (newFont)
            {
                Menu menu = PlayerMenus.GetMenuWithName ("Subtitles");
                foreach (MenuElement element in menu.elements)
                {
                    element.font = newFont;
                }
                menu.Recalculate ();
            }
        }
    
    
        [System.Serializable]
        private class PerLanguageFont
        {
    
            public int languageIndex;
            public Font font;
    
        }
    
    }
    
  • Awesome! Thanks!

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.