Search Unity

Question LocalizedString TableReferenceEntry change in editor - KeyId zero

Discussion in 'Localization Tools' started by Ahim13, Jan 23, 2024.

  1. Ahim13

    Ahim13

    Joined:
    Aug 15, 2016
    Posts:
    5
    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)
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Can you show the code you are using to do this?
     
  3. Ahim13

    Ahim13

    Joined:
    Aug 15, 2016
    Posts:
    5
    Code (CSharp):
    1. [MenuItem("CONTEXT/MyComponent/SetupID")]
    2.         public static void SetupID()
    3.         {
    4.             foreach (Transform selectedTransform in Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.Editable))
    5.             {
    6.                 var myComponent = selectedTransform.GetComponent<MyComponent>();
    7.                 myComponent.ID = new LocalizedString(myComponent.ID.TableReference, selectedTransform.name);
    8.                 EditorUtility.SetDirty(myComponent);
    9.             }
    10.         }
    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
     
    Last edited: Jan 23, 2024
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    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:

    Code (csharp):
    1. var collection = LocalizationEditorSettings.GetStringTableCollection(myComponent.ID.TableReference);
    2. var id = collection.SharedData.GetId(selectedTransform.name);
    3. myComponent.ID = new LocalizedString(myComponent.ID.TableReference, id);
     
  5. Ahim13

    Ahim13

    Joined:
    Aug 15, 2016
    Posts:
    5
    Oh, makes sense.

    It works perfectly now, thank you for the help!
     
    karl_jones likes this.