LocalizedString TableReferenceEntry change in editor - KeyId zero

I have a LocalizedString and I want to change the TableEntryReference, I can do that successfully, but the problem is that the KeyId remains 0 (like when it is set to None in the Editor), however when I set the LocalizedString from the inspector to the same TableEntryReference I set through script, the KeyId shows the correct value.

How can I update the KeyId to the new TableEntryReference in code?

(The goal here is to have one helper function in Editor as a Context MenuItem so I can assign the TableEntryReferences to all my objects, which mean 600+ objects and so I don’t have to do them manually)

Can you show the code you are using to do this?

[MenuItem("CONTEXT/MyComponent/SetupID")]
        public static void SetupID()
        {
            foreach (Transform selectedTransform in Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.Editable))
            {
                var myComponent = selectedTransform.GetComponent<MyComponent>();
                myComponent.ID = new LocalizedString(myComponent.ID.TableReference, selectedTransform.name);
                EditorUtility.SetDirty(myComponent);
            }
        }

Here MyComponent is a custom component that has an ID property with the type LocalizedString.
The name of the gameobject matches perfectly the TableEntryReference. It also show the correct localized string, just the KeyId remains 0.

I also set the TableReference manually, as that is easy to do from the editor selecting multiple objects

I think I see the problem.

A table reference can either be an id or a name. The TableReference struct handles either for you, when you assign it to a LocalizedString it will serialize whichever method is being used. So it sounds like the TableEntryReference.Type is Name.
In the inspector, we use Id as its safer. To extract the ID you need the SharedTable data, you can then convert the Name reference into an ID.

You need to get the table collection: GetStringTableCollection

Something like:

var collection = LocalizationEditorSettings.GetStringTableCollection(myComponent.ID.TableReference);
var id = collection.SharedData.GetId(selectedTransform.name);
myComponent.ID = new LocalizedString(myComponent.ID.TableReference, id);

Oh, makes sense.

It works perfectly now, thank you for the help!

1 Like