Custom editor save serializedProperties on PlayMode

Hi,

I have a class with a custom editor attached to it.

In the inspector, I set a button that change a property of the object.

For example:

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class TextExample : MonoBehaviour
{
    [SerializeField]
    public string  _Text;
}

#if UNITY_EDITOR
[CustomEditor(typeof(TextExample))]
public class TextExampleEditor : Editor
{
    TextExample _targetProp;
    SerializedProperty _textProp;

    void OnEnable()
    {
        _targetProp = (TextExample) target;
        _textProp = serializedObject.FindProperty("_Text");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUI.BeginChangeCheck();

        if (GUILayout.Button("Click Me"))
        {
            Undo.RecordObject(target, "Text have been changed");
            _textProp.stringValue ="EXAMPLE TEXT";
        }
        if (EditorGUI.EndChangeCheck())
        {
            // arr.Refresh();
        }
        serializedObject.ApplyModifiedProperties();
        DrawDefaultInspector();
    }
}
#endif

In Edit mode, when clicking the button, the value is correctly set to “EXAMPLE TEXT”. And when enter in play mode, the value persist.

But the converse ( PlayMode + click the button + exit PlayMode) is not true. I somewhat expected this behavior because Unity doesn’t persist changes made on Play Mode.

Is there some kind of workaround to be able to save during PlayMode? (I tested cinemachine attribute [SaveOnPlay] but doesn’t work in my example)

A workaround would be to persist the data on disk