Search Unity

Unable to ctrl+z to undo when editing the field.

Discussion in 'UI Toolkit' started by Kidodelog, Sep 6, 2019.

  1. Kidodelog

    Kidodelog

    Joined:
    Mar 4, 2015
    Posts:
    5
    The editor is unable to perform undo(ctrl+z) when editing the field. (e.g. PropertyField, TextField, IntegerField )

    Is this by design or I did something wrong?

    Kindly please advice. Thanks.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ItemCollection : MonoBehaviour {
    4.  
    5.     [SerializeField]
    6.     private int quantity = 0;
    7.  
    8.     void Start () {
    9.         Debug.LogFormat("Quantity : {0}", quantity);
    10.     }
    11. }
    12.  
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEngine.UIElements;
    4. using UnityEditor.UIElements;
    5.  
    6. [CanEditMultipleObjects]
    7. [CustomEditor(typeof(ItemCollection))]
    8. public class ItemCollectionEditor : Editor {
    9.  
    10.     private SerializedProperty quantityProp;
    11.  
    12.     void OnEnable () {
    13.         quantityProp = serializedObject.FindProperty("quantity");
    14.     }
    15.  
    16.     // Work fine when using IMGUI.
    17.     /*
    18.     public override void OnInspectorGUI () {
    19.         serializedObject.Update();
    20.  
    21.         EditorGUILayout.PropertyField(quantityProp);
    22.  
    23.         serializedObject.ApplyModifiedProperties();
    24.     }
    25.     */
    26.  
    27.     // Unable to perform ctrl+z to undo when editing the field using UIElement.
    28.     public override VisualElement CreateInspectorGUI () {
    29.         var root = new VisualElement();
    30.  
    31.         var quantityField = new PropertyField(quantityProp);
    32.         quantityField.Bind(serializedObject);
    33.         root.Add(quantityField);
    34.  
    35.         return root;
    36.     }
    37.  
    38. }
    39.  
     
    Last edited: Sep 6, 2019
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @Kidodelog,

    Thanks a lot for the feedback, you are right UIElements fields should behave the same as IMGUI's. Could you report this as a bug?
     
  3. Kidodelog

    Kidodelog

    Joined:
    Mar 4, 2015
    Posts:
    5
    Hi @hugobd,

    Just submit the bug report. Thanks.
     
    Last edited: Sep 7, 2019
  4. FeXyK

    FeXyK

    Joined:
    Oct 30, 2020
    Posts:
    6
    Any update on this?
     
  5. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @FeXyK!

    Unfortunately, undo / redo is currently not supported with the UITK textfield. We are still tracking it and we plan to implement it, hopefully shortly.

    I know this is not the same, but in the meantime, using ESC will revert the TextField to its original value.
     
    FeXyK likes this.
  6. javierfed

    javierfed

    Joined:
    Apr 10, 2015
    Posts:
    50
    any update on this? would love to not have to tell people you can't undo this, can't undo that.
     
  7. javierfed

    javierfed

    Joined:
    Apr 10, 2015
    Posts:
    50
    is there any way to setup undo/redo on normal UITK fields like the checkbox, intSlider, etc?
     
  8. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    For Editor UI, using Serialized Property Bindings will give you Undo/Redo out of the box.

    This is because SerializedObject are automatically monitored by Unity for changes to their properties. The Undo/Redo system can access this and change the object direclty, which will update the UI thanks to bindings.

    When doing something else than Editor UI, there is no support for Undo/Redo. It's up to you to implement it on top of the fields (use change events to monitor their value and manage the undo stack, handle keyboard shortcuts, etc.).
     
  9. javierfed

    javierfed

    Joined:
    Apr 10, 2015
    Posts:
    50
    so to make an editor that previews things or is used to tweak things but not bound to a particular object then we have to figure out how to add undo/redo to them manually?

    would making a "Scriptable Object" be sufficient to get the undo/redo logic to be inherent?
     
    Fly81 likes this.
  10. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Yes, creating a subclass of ScriptableObject with the fields that you want to support Undo/Redo and binding with Serialized Property Bindings to the Visual Element should give you this out of the box.

    You can also use install your own callbacks on the fields, modify the fields of the ScriptableObject manually and register Undo changes with the Undo.RecordObject method.