Apparently, Unity already supports getting all the text in a scene and creating table entries for you in the new localization pacakge.
My game is in unity 2019.4 LTS version and AFAIK that feature isn’t available. I tried to write a script that will create a table entry for a textmeshpro component and also assign it in the LocalizedStringEvent components event.
var ls = new LocalizedString();
var key = "1";
var tmp = GetComponent<TextMeshProUGUI>();
foreach (var table in s.StringTables)
{
table.AddEntry(key, tmp.text);
}
ls.TableReference = s.TableCollectionName;
ls.TableEntryReference = key;
GetComponent<LocalizeStringEvent>()
.StringReference.SetReference(s.TableCollectionName,key);
PropertyInfo stringProp = typeof(TextMeshPro).GetProperty(nameof(tmp.text));
Debug.Log(stringProp + " " + stringProp.GetSetMethod());
UnityAction<string> textsetDelegate = (
UnityAction<string>) Delegate.CreateDelegate(typeof(UnityAction<string>),
tmp, stringProp.GetSetMethod());
UnityEditor.Events.UnityEventTools.AddPersistentListener<String>(
GetComponent<LocalizeStringEvent>().OnUpdateString,
textsetDelegate
);
This works works but there might be issues. Which is why I’m asking for help.
If there are none, I’ll update this to work with my code to fetch text mesh pro components in scene and turn this into something usable for everyone.
In the meantime, I hope the feature is backported to LTS versions.
Edit:
Well, it’s done. It’s super barebones but sufficient for my use cases. I hate writing editor GUI code so I won’t be extending it further.
It’s a wizard that will fetch textmeshpro and textmeshproUGUI components in a scene. You can chose to ignore certain components if you want. Clicking on an entry will take you to the game object.
It will add the built-in LocalizedStringEvent component, create a table entry and assign it and also set up the Unity event for the LocalizedStringEvent component’s OnStringUpdate().
.
All the table entries will have the current text of the textmeshPro component.
If the gameobject already has a LocalizedStringEvent attached, it will replace the string reference and also add to the UnityEvent (and possibly create duplicate callbacks). But that should be harmless.
If nothing else, it should be useful for navigating the scene to find TextMeshpro Localized strings and hopefully cutdown some menial work.
Reqs:
Localization package: 0.11.1
Textmeshpro (version shouldn’t be an issue)
7571959–938365–LocalizationHelperWizard.unitypackage (3.67 KB)
