Auto-Filled LocalizedString Resets on Restart Editor

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: ”.

How can I fix this issue?

Here is the responsible code:

public static void UpdateLocalizationEntry(LocalizedString? text, StringTable? table, string? key)
{
    if (table == null || text == null || string.IsNullOrEmpty(key!))
    {
        return;
    }

    StringTableEntry? existingEntry = table.GetEntry(key);

    if (existingEntry == null)
    {
        StringTableEntry newEntry = table.AddEntry(key, "")!;
        text.SetReference(table.SharedData!.TableCollectionNameGuid, newEntry.Key);
        EditorUtility.SetDirty(table.SharedData);
        EditorUtility.SetDirty(table);

        return;
    }

    if (!text.ContainsKey(key))
    {
        text.SetReference(table.SharedData!.TableCollectionNameGuid, existingEntry.Key);
        EditorUtility.SetDirty(table.SharedData);
        EditorUtility.SetDirty(table);
    }
}

// This code is pretty much just iterating over all the dialogues and calling UpdateLocalizationEntry on them.
private void UpdateLocalizationIds()
{
    DialogueEntry[]? dialogueEntries = DialogueEntries;

    // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
    if (dialogueEntries == null)
    {
        return;
    }

    for (int i = 0; i < dialogueEntries.Length; i++)
    {
        string id = GetExpectedDialogueLocalizationId(i);
        DialogueEntry dialogueEntry = dialogueEntries[i];
        LocalizedString text = dialogueEntry.LocalizedDialogueText;

        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
        if (text == null)
        {
            dialogueEntry.LocalizedDialogueText = new LocalizedString();
            text = dialogueEntry.LocalizedDialogueText;
        }

        StringTableCollection sharedDataTable =
            LocalizationEditorSettings.GetStringTableCollection(DialogueTableName)!;

        ReadOnlyCollection<StringTable>? languageTables = sharedDataTable!.StringTables;

        if (languageTables == null)
        {
            continue;
        }

        foreach (StringTable languageTable in languageTables)
        {
            EditorLocalizationUtility.UpdateLocalizationEntry(text, languageTable, id);
            EditorUtility.SetDirty(languageTable);
        }
        
        EditorUtility.SetDirty(sharedDataTable.SharedData!);
        EditorUtility.SetDirty(sharedDataTable);
    }
    
    AssetDatabase.SaveAssets();
}

Ah I solved it
I forgot to set the ScriptableObject to dirty after I set the LocalizedString :roll_eyes:

UnityEditor.EditorUtility.SetDirty(this);
1 Like