Request for VisualElement OnDestroy or OnRemoved event

Hi,

I think that it would be a good thing to add a OnDestroy or OnRemoed event on the VisualElement class.
Here is the reason why.

Imagine you have a simple integerField field with a UndoRecord:

var integerField = new IntegerField("test Int");
integerField.isDelayed = true;
integerField.value = exampleObject.testInt;
integerField.RegisterValueChangedCallback(c => {
               
   Undo.RecordObject(exampleObject, exampleObject.name);
   exampleObject.testInt = c.newValue;
                
});

If possible I would like to add a undoRedoCallBack that updates the value:

Undo.undoRedoPerformed += () => {
   integerField.value = exampleObject.testInt;
};

But what if the integerField is an element in a list where you can add and remove elements. I don’t want the undoredoCallback to update an intergerField that is no longer displayed.
So it would be great if I could do something around the lines of:

Undo.UndoRedoCallback updateValue = () => {
  integerField.value = exampleObject.testInt;
};
Undo.undoRedoPerformed += updateValue;
integerField.onRemove += () => { Undo.undoRedoPerformed -= updateValue; };

Please do let me know if I am overcomplicating things and if there is a simpler solution to make a correctly functioning undoredo value field without binding (I can’t use binding because I am using abstract/generic classes and therefore its too complicated to get the object propertyPath of each property, I prefer setting the values manually)

Thank you for your time

1 Like

Does this help?

1 Like

Thank you!
This works great:

 integerField.RegisterCallback<DetachFromPanelEvent>(c => { Undo.undoRedoPerformed -= updateValue; });

I guess I should have read the UIElements Event documentation more attentively:

2 Likes