- Home /
Custom Inspector Undo Functionality when directly changing SerializedProperties
Hello, I'm relatively new at working with custom Inspector/Editor. The serialized properties have been very helpful for me since they include the automatic undo functionality. But this doesn't seem to work when you edit the value of the serialized property directly. (in this case .boolValue)
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Set Selection on Activation", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
bool _tryFind = tryFindSelectableInChildren.boolValue;
if(_tryFind) _selected =1;
else _selected = 0;
serializedObject.Update();
_selected = EditorGUILayout.Popup(new GUIContent
("Select on Activation", "shortened"),_selected,popupContent);
EditorGUILayout.BeginVertical(EditorStyles.objectField);
if(_selected==0)
{
_tryFind = false;
EditorGUILayout.PropertyField(preSelected);
}
else if(_selected==1)
{
_tryFind = true;
EditorGUILayout.PropertyField(parentOfSelectables,new GUIContent("Set Parent"));
}
tryFindSelectableInChildren.boolValue = _tryFind;
EditorGUILayout.EndVertical();
serializedObject.ApplyModifiedProperties();
}
How do I record the changes I'm doing to "tryFindSelectableInChildren" in my undo function?
Answer by grimprophecy · Feb 06, 2021 at 09:39 PM
Assign the bool to a temporary variable, then in your EndChangeCheck, record and set _tryFind like this example:
https://docs.unity3d.com/ScriptReference/Undo.RecordObject.html
That should work, but you might not need Undo.RecordObject for value types like bools, I can't remember off the top of my head.
That doesn't work.
If I put my serialized property in the Undo.Record Object I get an error:
Argument "1": Conversion of "UnityEditor.SerializedProperty" in "UnityEngine.Object" not possible. [Assembly-CSharp-Editor]
The problem here is that I'm not accessing some public property of my script directly, but I'm using the SerializedProperties, so recording the script is kinda pointless since I can't actually change the value of the property that way. (I think)
Actually I tried it for the hell of it and it seems to work. I don't understand why, but it works...
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
EscapeFromEarth.UI.$$anonymous$$enu _menu = (EscapeFromEarth.UI.$$anonymous$$enu)target;
EditorGUILayout.LabelField("Set Selection on Activation", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
bool _tryFind = tryFindSelectableInChildren.boolValue;
if(_tryFind) _selected =1;
else _selected = 0;
serializedObject.Update();
_selected = EditorGUILayout.Popup(new GUIContent("Select on Activation", "shortened"),_selected,popupContent);
EditorGUILayout.BeginVertical(EditorStyles.objectField);
if(_selected==0)
{
_tryFind = false;
EditorGUILayout.PropertyField(preSelected);
}
else if(_selected==1)
{
_tryFind = true;
EditorGUILayout.PropertyField(parentOfSelectables,new GUIContent("Set Parent"));
}
//tryFindSelectableInChildren.boolValue = _tryFind;
EditorGUILayout.EndVertical();
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(_menu, "Set Field Value");
tryFindSelectableInChildren.boolValue = _tryFind;
}
serializedObject.Apply$$anonymous$$odifiedProperties();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613024714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom inspector drop down 0 Answers
Cannot get a class to show up correctly in a custom inspector 0 Answers
Can not you change the default GUI style of EditorGUI in OnInspectorGUI? 1 Answer
Custom editor super slow even with no draw operations 1 Answer
Automatically increment an integer field in a bunch of prefab gameobjects in the unity editor. 0 Answers