- Home /
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)
Your answer

Follow this Question
Related Questions
How can I use a GUIStyle in a Custom Editor which is set within that Custom Editor? 1 Answer
Finding property with serializedObject on script with a generic 0 Answers
SerializedObject.FindProperty returning null 2 Answers
How to get an array inside a simple class at Editor window and change arraysize? 0 Answers
Serializedproperty assignment 2 Answers