Page 1 of 1

Can Text Table fields include variables for dynamic text?

Posted: Wed Dec 23, 2020 11:50 pm
by Andy Miira
Hi! Let's say I have a text table field, "Game Version", I want it to show the following text in my UI:

Code: Select all

Game version: 1.0.0
But instead of the 1.0.0, I want it to fetch the game version dynamically, using Unity's "Application.version".

Is there any way to set custom dynamic variables inside Text Table fields?
Should I use Lua functions for that purpose?

Re: Can Text Table fields include variables for dynamic text?

Posted: Thu Dec 24, 2020 8:48 am
by Tony Li
Hi,

I'm not sure that you'd want to use text tables or Lua functions for that. But if you want to, you can register a Lua function to return the current version:

Code: Select all

Lua.RegisterFunction("GetAppVersion", null, SymbolExtensions.GetMethodInfo(() => GetAppVersion()));

static string GetAppVersion() { return Application.version; }
Then make a subclass of LocalizeUI that overrides GetLocalizedText() to parse [var=variable] and [lua(code)] tags:

Code: Select all

public class LocalizeUIWithTags : PixelCrushers.LocalizeUI
{
    protected override string GetLocalizedText(string fieldName)
    {
        return FormattedText.ParseCode(base.GetLocalizedText(fieldName));
    }
}
(Edit: Fixed typo.)

Then you can set a UI Text or TextMeshProUGUI field to:

Code: Select all

Game version: [lua(GetAppVersion())]

Re: Can Text Table fields include variables for dynamic text?

Posted: Thu Dec 24, 2020 12:15 pm
by Andy Miira
I want to use Text Tables in this case, because I also needed to localize the "Game Version:" part of the string field.

Overall, I wanted the option to parse dynamic variables/Lua code inside Text Table fields (similarly to the Dialogue texts), and the code you posted works great!
Thanks :D

I just had to change the override access to "protected", and it all worked perfectly!

Code: Select all

protected override string GetLocalizedText(string fieldName)

Re: Can Text Table fields include variables for dynamic text?

Posted: Thu Dec 24, 2020 12:59 pm
by Tony Li
Oops; typo. I just fixed it in my post above.