I have a custom String class called LocalizedString, with a custom property drawer that has a button that opens up a custom editor window.
The editor window finds all strings in a database, shows them in a list, with a button after each.
Clicking that button should send that database entry back into the property window.
I’ve gotten it to work somewhat, but it has some weird behavior.
edit: To try and understand it better, imagine a picker element, like for a material, or GameObject, or similar, but a custom one for strings from a big list. That’s what I’m trying to make.
The top script is the database, it shows a key (‘test’) and a value (‘this is a test string’).
You can add more, shuffle them around, etc.
The bottom script is a demo script of the localization stuff.
It looks like this:
public class DemoGlitchLocalization : MonoBehaviour
{
public LocalizedString string1;
public LocalizedString string2;
public LocalizedString[] stringArray;
}
You can see that the custom property drawer is shown, with the find button at the end.
The property drawer has the following code in OnGUI:
string pending;
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
{
SerializedProperty text = prop.FindPropertyRelative("text");
label = EditorGUI.BeginProperty(pos, label, prop);
... // [label and text field]
if (GUI.Button(new Rect(pos.x + pos.width * 0.9f, pos.y, pos.width * 0.1f, pos.height), new GUIContent("find", "Find in GlitchLocalization")))
{
GlitchLocalizationWindow.Show(this);
}
if (!string.IsNullOrEmpty(pending))
{
text.stringValue = pending;
pending = null;
}
EditorGUI.EndProperty();
}
public void SetValue(string value)
{
pending = "$" + value;
}
This function is described as:
LocalizedStringDrawer drawer;
public static void Show(LocalizedStringDrawer drawer)
{
EditorWindow window = EditorWindow.GetWindow(typeof(GlitchLocalizationWindow));
(window as GlitchLocalizationWindow).drawer = drawer;
}
So I’m passing a reference to the property drawer when the editor window is shown. It looks like this:
It has the same entry as the GlitchLocalization script. Clicking the checkmark does the following:
void SaveSelection(string selection)
{
drawer.SetValue(selection);
Close();
}
So it sends the selection string back to the property drawer that opened the window. Setvalue is defined in the property drawer (see above).
Now the first problem:
It doesn’t automatically refresh the property drawer. I have to click outside of the property, before the Inspector refreshes and sees that the value is changed.
For String1 and String2 in the demo script, the value is put in the right place, the only problem is the refresh.
The second problem
Once I place the values in an Array, the value will always be put in the first item of the array, even when clicking the find button on properties that are further down. If you would click on the find button behind Element1 in the String Array of the demo script, and select a value, it will place it in Element0.
I’m guessing there’s a better way to do all of this, and I’d love to hear it.