Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Auto-Filled LocalizedString Resets on Restart Editor

Discussion in 'Localization Tools' started by kdserra, Oct 28, 2023.

  1. kdserra

    kdserra

    Joined:
    May 16, 2018
    Posts:
    10
    I have an an editor button that will auto fill the localization ids so designers can focus on just adding content instead of finding the correct id for dialogue entries (which varies based on the story, current level, and dialogue index for that level).

    This button correctly creates or assigns LocalizedString if it doesn't/already exists.

    The issue is that when I restart Unity, the assigned LocalizedString is reset to None (String) with a warning below it "Could not find a Table Collection with the Guid: <GUID>".

    How can I fix this issue?



    Here is the responsible code:
    Code (CSharp):
    1.  
    2. public static void UpdateLocalizationEntry(LocalizedString? text, StringTable? table, string? key)
    3. {
    4.     if (table == null || text == null || string.IsNullOrEmpty(key!))
    5.     {
    6.         return;
    7.     }
    8.  
    9.     StringTableEntry? existingEntry = table.GetEntry(key);
    10.  
    11.     if (existingEntry == null)
    12.     {
    13.         StringTableEntry newEntry = table.AddEntry(key, "")!;
    14.         text.SetReference(table.SharedData!.TableCollectionNameGuid, newEntry.Key);
    15.         EditorUtility.SetDirty(table.SharedData);
    16.         EditorUtility.SetDirty(table);
    17.  
    18.         return;
    19.     }
    20.  
    21.     if (!text.ContainsKey(key))
    22.     {
    23.         text.SetReference(table.SharedData!.TableCollectionNameGuid, existingEntry.Key);
    24.         EditorUtility.SetDirty(table.SharedData);
    25.         EditorUtility.SetDirty(table);
    26.     }
    27. }
    28.  
    29. // This code is pretty much just iterating over all the dialogues and calling UpdateLocalizationEntry on them.
    30. private void UpdateLocalizationIds()
    31. {
    32.     DialogueEntry[]? dialogueEntries = DialogueEntries;
    33.  
    34.     // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
    35.     if (dialogueEntries == null)
    36.     {
    37.         return;
    38.     }
    39.  
    40.     for (int i = 0; i < dialogueEntries.Length; i++)
    41.     {
    42.         string id = GetExpectedDialogueLocalizationId(i);
    43.         DialogueEntry dialogueEntry = dialogueEntries[i];
    44.         LocalizedString text = dialogueEntry.LocalizedDialogueText;
    45.  
    46.         // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
    47.         if (text == null)
    48.         {
    49.             dialogueEntry.LocalizedDialogueText = new LocalizedString();
    50.             text = dialogueEntry.LocalizedDialogueText;
    51.         }
    52.  
    53.         StringTableCollection sharedDataTable =
    54.             LocalizationEditorSettings.GetStringTableCollection(DialogueTableName)!;
    55.  
    56.         ReadOnlyCollection<StringTable>? languageTables = sharedDataTable!.StringTables;
    57.  
    58.         if (languageTables == null)
    59.         {
    60.             continue;
    61.         }
    62.  
    63.         foreach (StringTable languageTable in languageTables)
    64.         {
    65.             EditorLocalizationUtility.UpdateLocalizationEntry(text, languageTable, id);
    66.             EditorUtility.SetDirty(languageTable);
    67.         }
    68.        
    69.         EditorUtility.SetDirty(sharedDataTable.SharedData!);
    70.         EditorUtility.SetDirty(sharedDataTable);
    71.     }
    72.    
    73.     AssetDatabase.SaveAssets();
    74. }
    75.  
     
  2. kdserra

    kdserra

    Joined:
    May 16, 2018
    Posts:
    10
    Ah I solved it
    I forgot to set the ScriptableObject to dirty after I set the LocalizedString :rolleyes:

    Code (CSharp):
    1. UnityEditor.EditorUtility.SetDirty(this);
     
    karl_jones likes this.