Hi,
I have been using Localisation package for a little bit now to swap my current project localisation with the one from unity itself.
So far it worked great but i cant figure out how to set an object in the update string event through code.
For context I had a json file with keys and values that was used for localisation, then the script would use the text from its text component as key, and get the translated value back.
Now to change this i made a script that adds the string event component to the object, add the table and string reference based on the text from its text component. this all works but now i cant figure out how to add the text to the update string part. Anyone know how i can do this?
One thing to note is that this scrips does everything in editor time.
Line 122 is what im talking about.
Thanks in advance!
PS: I did it with hand now, but this should happen automatically since its hundreds of different objects:
using UnityEngine;
using UnityEngine.Localization;
using UnityEditor;
using UnityEditor.Localization;
using UnityEngine.Localization.Tables;
using UnityEngine.UI;
using UnityEngine.Localization.Components;
using UnityEngine.Localization.Settings;
public class LocalizedTextReplacer : EditorWindow
{
[SerializeField] private string tableName;
[MenuItem("Tools/Localized Text Replacer")]
public static void ShowWindow()
{
GetWindow<LocalizedTextReplacer>("Localized Text Replacer");
}
private void OnGUI()
{
GUILayout.Label("Replace LocalizedText Components", EditorStyles.boldLabel);
tableName = EditorGUILayout.TextField("Table Name", tableName);
if (GUILayout.Button("Update Localized Text Components"))
UpdateLocalizedTextComponents();
}
private void UpdateLocalizedTextComponents()
{
Debug.Log("Started replacing Localized Text");
if (ValidateTableName())
{
var localizedTextComponents = FindLocalizedTextComponents();
ProcessLocalizedTextComponents(localizedTextComponents);
}
}
private bool ValidateTableName()
{
if (string.IsNullOrEmpty(tableName))
{
Debug.LogWarning("Table name cannot be empty.");
return false;
}
var table = GetStringTable();
if (table == null)
{
Debug.LogWarning($"Localization table '{tableName}' does not exist.");
return false;
}
Debug.Log($"StringTable '{tableName}' was found.");
return true;
}
private LocalizationTable GetStringTable()
{
var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
if (stringTableCollections.Count > 0)
{
Debug.Log($"{stringTableCollections.Count} collections found");
var collection = stringTableCollections[0]; // Assuming there is only one collection right now
var table = collection.GetTable("nl"); // This works correctly for accessing the localization table
if (table != null)
{
return table;
}
else
{
Debug.LogWarning("Table not found in collection.");
}
}
else
{
Debug.LogWarning("No string table collections found.");
}
return null;
}
private LocalizedText[] FindLocalizedTextComponents()
{
var localizedTextComponents = FindObjectsOfType<LocalizedText>(true);
Debug.Log($"Found '{localizedTextComponents.Length}' components to replace.");
return localizedTextComponents;
}
private void ProcessLocalizedTextComponents(LocalizedText[] localizedTextComponents)
{
foreach (var localizedText in localizedTextComponents)
{
var textComponent = localizedText.GetComponentInChildren<Text>();
if (textComponent != null)
{
ReplaceLocalizedTextComponent(localizedText, textComponent);
}
else
{
Debug.LogWarning($"No Text component found for LocalizedText on GameObject '{localizedText.gameObject.name}'");
}
}
}
private void ReplaceLocalizedTextComponent(LocalizedText localizedText, Text textComponent)
{
string textValue = textComponent.text;
var localizeStringEvent = localizedText.gameObject.AddComponent<LocalizeStringEvent>();
localizeStringEvent.SetTable(tableName);
localizeStringEvent.SetEntry(textValue);
//TODO add the textComponent to the Update string
MarkObjectDirty(localizedText.gameObject);
Debug.Log($"Updated LocalizedText on GameObject '{localizedText.gameObject.name}'");
DestroyImmediate(localizedText);
}
private void Test(string stringtest)
{
}
private void MarkObjectDirty(GameObject obj)
{
EditorUtility.SetDirty(obj);
Debug.Log($"Marked '{obj.name}' as dirty.");
}
}