Reading metadata at runtime

How can I read metadata at runtime?

I made a custom metadata class for holding the Rect transform size of my text and need a way to access the metadata so I can use it after a string is set, but I can’t seem to figure out how

public class UpdateLocalizedString : MonoBehaviour
{
    public LocalizeStringEvent stringToUpdate;

    public LocalizedString[] strings;


    public void UpdateReference(int i)
    {
        stringToUpdate.StringReference = strings[i];
        var currentLoadingOperation = strings[i].CurrentLoadingOperation;
        if (currentLoadingOperation.HasValue)
        {
            currentLoadingOperation.Value.Completed += ValueOnCompleted;
        }
    }

    private void ValueOnCompleted(AsyncOperationHandle<LocalizedDatabase<StringTable, StringTableEntry>.TableEntryResult> handle)
    {
        var rectData = handle.Result.Entry.GetMetadata<RectSizeMetaData>();
        Debug.LogWarning(rectData.Size);
    }
}

Is this correct?
I suppose i need to do an isDone check tho because completed probably wont fire if its already loaded into memory by chance i suppose

Hi,

It depends where the metadata is stored, I assume its in the entry for the locale so that would be like this

        // From an entry
        var operation = LocalizationSettings.StringDatabase.GetTableEntryAsync("My Table", "My Entry");
        yield return operation;
        var entry = operation.Result.Entry;
        entry.GetMetadata<MyMetadata>();

We actually have a solution for custom RectTransform properties for each locale. Ill be showing it on Thursday at the Webinar Localization Package Beta Webinar and Q&A

Edit:

Yes that looks correct. You will need to wait for the operation to complete.
The completed event should fire even if its already completed. Addressables will handle this by calling the event again during its next update when a new subscriber is added.

Oh that’s awesome! Cant wait to see it

But awesome, thanks for the reply!
The way you showed also looks useful

1 Like