Hi everyone,
as the title already says, I’m looking for a way to get/set values on a LocalizedString by accessing it via the SerializedObject / SerializedProperty in a CustomPropertyDrawer. The Attribute is not used on the LocalizedString itself, but on a long-field, the localized-string name is provided as string in the attribute.
the targetProperty is of type generic, how can I access the LocalizedString and assign new TableReference and TableEntryReferences in this context?
Can I somehow access the “real” object from the serializedProperty?
As far as I know, only referenced UnityEngine.Objects are accessible.
Is my only option to use some workaround like mentioned here:
The LocalizedString has a custom property drawer that will provide an Editor for selecting.
If you need to specifically get and set its values then you can access its serialized properties.
You can look at the source code for reference. You would want to look at LocalizedReference.cs.
There are 2 properties you want to access:
Inside of m_TableReference you want the child property m_TableCollectionName.
so something like:
var targetProperty = property.serializedObject.FindProperty(localizedStringTarget);
targetProperty.FindPropertyRelative("m_TableReference.m_TableCollectionName").stringValue = "My Table";
To change the entry
var targetProperty = property.serializedObject.FindProperty(localizedStringTarget);
targetProperty.FindPropertyRelative("m_TableEntryReference.m_Key").stringValue = "My Table Entry";
// or
targetProperty.FindPropertyRelative("m_TableEntryReference.m_KeyId").longValue= 12345; // When using the Key Id
Ah thanks! My mistake was to trying to find the public Property TableReference.
Looking into the LocalizedReference code that Property also calls a ForceUpdate()-Function, do I also need to call this somehow from outside when accessing the fields directly for a valid editor state or is this only relevant at runtime? And if I need to force update how can I do this in the serializedProperty context, is there some helper to update all?
You should not need to call that. Its used to load a new localized string. We also have some checks in serialization that detect these type of changes and call force update for you.
Hey, I stumbled upon this thread as I’m working on a custom Property Attribute to just display a localized string as a text area for quick modification (instead of having to open bunch of foldouts).
This is my current implementation, but it doesn’t handle the case where the LocalizedString is empty.
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.type == "LocalizedString")
{
var entryReference = property.FindPropertyRelative("m_TableEntryReference.m_KeyId");
var collection = LocalizationEditorSettings.GetStringTableCollection("Text Localization");
var table = collection.GetTable("en") as StringTable;
var entry = table.GetEntry(entryReference.longValue);
if (entry == null)
{
EditorGUILayout.PropertyField(property);
return;
}
if (previousValue == "") previousValue = entry.Value;
Undo.RecordObject(table, "val");
Undo.RecordObject(table.SharedData, "val");
GUILayout.BeginHorizontal();
GUILayout.Label(property.displayName, GUILayout.MaxWidth(150));
entry.Value = EditorGUILayout.TextArea(entry.Value, GUILayout.Height(100));
GUILayout.EndHorizontal();
if (entry.Value != previousValue)
{
EditorUtility.SetDirty(table);
EditorUtility.SetDirty(table.SharedData);
}
}
else
{
EditorGUILayout.HelpBox("SimplifyLocalization works only on LocalizedString", MessageType.Warning);
}
}
I tried to access the LocalizedString itself through property.boxedValue, but that just caused unity to crash unfortunately.
I’ve tried to get the property.FindPropertyRelative("m_TableEntryReference") into a separate variable, but it’s always showing a TableEntryReference(Empty).
Any ideas how to check whether the LocalizedString is empty or not through a SerializedProperty?
EDIT: Seems like entry == null seems to be working.
Additionally, since I’m using a property attribute based drawer for a LocalizedString, I think the attribute’s property drawer is overriding the normal LocalizedString property drawer, causing the property to look like this when using EditorGUILayout.PropertyField(property);:
You need to check if m_TableReference.m_TableCollectionName and m_TableEntryReference.m_Key or is null or empty and if m_TableEntryReference.m_KeyId is not zero.