Search Unity

Feature Request Request for VisualElement OnDestroy or OnRemoved event

Discussion in 'UI Toolkit' started by Sangemdoko, Jul 30, 2019.

  1. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    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:

    Code (CSharp):
    1. var integerField = new IntegerField("test Int");
    2. integerField.isDelayed = true;
    3. integerField.value = exampleObject.testInt;
    4. integerField.RegisterValueChangedCallback(c => {
    5.                
    6.    Undo.RecordObject(exampleObject, exampleObject.name);
    7.    exampleObject.testInt = c.newValue;
    8.                
    9. });
    If possible I would like to add a undoRedoCallBack that updates the value:

    Code (CSharp):
    1. Undo.undoRedoPerformed += () => {
    2.    integerField.value = exampleObject.testInt;
    3. };
    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:

    Code (CSharp):
    1. Undo.UndoRedoCallback updateValue = () => {
    2.   integerField.value = exampleObject.testInt;
    3. };
    4. Undo.undoRedoPerformed += updateValue;
    5. 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
     
    AdamBebko likes this.
  2. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    862
  3. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    IndieForger and AdamBebko like this.