- Home /
Custom editor: undo change to editor property
I'm writing a custom editor to quickly define paths in the Scene view: basic functionality is to select a point in the path, move it around, add a line starting from the selected point and so on.
How can I add an Undo step when the user selects a different point in the path? This is exactly what the Unity Editor does when the selected GameObject changes.
Here is my code for the selection logic, in my editor class:
// 'Curve' is my MonoBehaviour which defines a path in the game world.
[SerializeField]
private Curve curve_selected = null; // reference to the selected curve
[SerializeField]
private int point_selected_index = 0; // index of the selected point in the curve
private Vector3 ShowPoint(Curve curve, int point_index)
{
Vector3 point = curve.GetControlPoint(point_index); // gets the point in world space
Handles.color = Color.white;
if (Handles.Button(point, Quaternion.identity, 0.04f, 0.06f, Handles.DotCap))
{
// point has been clicked in the Scene view: select it.
//Undo.RecordObject(this, "Point Change"); // doesn't work! ('this' is the editor class)
this.curve_selected = curve;
this.point_selected_index = point_index;
Repaint();
}
return point;
}
My editor already supports Undo for changes to the path (i.e. the Curve script); since the reference to the selected point in the path is in my editor class (the 'curve_selected' and 'point_selected_index' fields), I've been unable to record the change to the editor.
I would be able to add the Undo step if I moved the reference to my path script, but it doesn't seem the right place to me: the point selection logic is only relevant in the Editor, not during gameplay.
Did you try wrapping it in an EditorGUI.BeginChangeCheck?
EditorGUI.BeginChangeCheck();
if (Handles.Button(point, Quaternion.identity, 0.04f, 0.06f, Handles.DotCap))
{
// point has been clicked in the Scene view: select it.
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(this, "Point Change"); //might need to be "target"
this.curve_selected = curve;
this.point_selected_index = point_index;
Repaint();
}
}
(That code is off the top of my head, so might need adjusting)
Tested now, does not work and stops the point selection from working. Likely EditorGUI.EndChangeCheck returns false even if the Handles.Button is pressed; to clarify, the above code is called in OnSceneGUI.
Hmm, yeah sorry. On reflection, the ChangeCheck isn't wrapping anything that's changing. You can use Begin/EndChangeCheck in OnSceneGUI though.
I agree that your path selection variable really belongs in the editor class, but as a work around you could put it in your path script surrounding by #if UNITY_EDITOR // #endif
Hopefully someone has solution!
Your answer
Follow this Question
Related Questions
How to properly handle Undo events in custom inspector? 0 Answers
Undo asset replace? *emergency D:* 3 Answers
Auto-Selecting something else in Scene-View? 0 Answers
Selecting a range of hex tiles within radius of main tile 1 Answer
Editor Selection and getting a different currentActiveGameObject. 0 Answers