Search Unity

Undo API's performance is horrible/slow (demonstration script for reproduction)

Discussion in 'Editor & General Support' started by Stacklucker, Jul 2, 2022.

  1. Stacklucker

    Stacklucker

    Joined:
    Jan 23, 2015
    Posts:
    82
    Making a new thread for this problem because I'm sure people will stumble across this in the future.

    This is partially answering my own question in a thread I posted yesterday (for reference: https://forum.unity.com/threads/how...ng-down-over-time-until-scene-reload.1303092/ )

    Unity UndoAPI is not only slow, but the performance worsens over time the more calls to the API you make until you reload the scene or call Undo.ClearAll().

    Unfortunately we are not talking about a few milliseconds, but seconds to minutes of editor freezing if you instantiate or delete hundreds of new objects over time.

    Apart from the obvious reason why this sucks, I particularly find this annoying because Unity itself writes in its Submission Guidelines (2.5.1 Editor Extensions) that Undo functionality is a required feature for editor extensions.

    Well, what if that feature renders the tool useless because the whole editor freezes?

    For people curious to replicate the issue:

    Code (CSharp):
    1. public class TestInstantiation : MonoBehaviour
    2. {
    3.  
    4.     public GameObject Prefab;
    5.     public int Count;
    6.     public bool UseUndo;
    7.     public List<GameObject> Instances = new List<GameObject>();
    8.  
    9. }
    10.  
    Editor Inspector:
    Code (CSharp):
    1.  
    2. [CustomEditor(typeof(TestInstantiation))]
    3. public class TestInstantiationInspector : Editor
    4. {
    5.     private TestInstantiation _base;
    6.     private void OnEnable()
    7.     {
    8.         _base = (TestInstantiation)target;
    9.     }
    10.  
    11.     //try this with _base.Count == 1000 and check the difference between _base.UseUndo on and off.
    12.     //using the undo API the "create" and "delete" functions become so slow after a couple presses that the whole editor freezes for seconds, even minutes. this worsens over time.
    13.     public override void OnInspectorGUI()
    14.     {
    15.         base.OnInspectorGUI();
    16.  
    17.         if (GUILayout.Button("Create"))
    18.         {
    19.             System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
    20.             watch.Start();
    21.             for (int i = 0; i < _base.Count; i++)
    22.             {
    23.                 var go = PrefabUtility.InstantiatePrefab(_base.Prefab) as UnityEngine.GameObject;
    24.                 _base.Instances.Add(go);
    25.  
    26.                 if (_base.UseUndo)
    27.                     Undo.RegisterCreatedObjectUndo(go, "created instance");
    28.             }
    29.  
    30.             watch.Stop();
    31.             Debug.Log($"Create {_base.Count} instances in {watch.ElapsedMilliseconds} ms");
    32.         }
    33.         if (GUILayout.Button("Delete"))
    34.         {
    35.             System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
    36.             watch.Start();
    37.             int count = _base.Instances.Count;
    38.             for (int i = 0; i < _base.Instances.Count; i++)
    39.             {
    40.                 if (_base.UseUndo)
    41.                     Undo.DestroyObjectImmediate(_base.Instances[i]);
    42.                 else
    43.                     GameObject.DestroyImmediate(_base.Instances[i]);
    44.             }
    45.             _base.Instances.Clear();
    46.             watch.Stop();
    47.             Debug.Log($"Deleted {count} instances in {watch.ElapsedMilliseconds} ms");
    48.         }
    49.  
    50.         if (GUILayout.Button("Clear Undo stack"))
    51.         {
    52.             Undo.ClearAll();
    53.         }
    54.     }
    55. }
    56.  
    57.  
    Is there really no way to increase performance, Unity???
     
    mgear likes this.
  2. Er_Cheng

    Er_Cheng

    Joined:
    Sep 18, 2019
    Posts:
    1
    I have the same problem. Do you have any solution?thx