I’ve run into this issue and found somewhat of a solution.
I’m developing a custom 3d Tilemap tool, and supporting undo/redo is a painful process. Undo.RegisterCreatedObjectUndo
, Undo.DestroyObjectImmediate
are the major bottlenecks.
My solution is to not call these, and fake object creation and deletion. Essentially, when I instantiate a tile prefab, I don’t register its creation with Undo, but I do this:
GameObject clone = Instantiate(...);
clone.hideFlags = HideFlags.HideAndDontSave;
clone.SetActive(false);
Undo.RecordObject(clone, "Insert Tile");
clone.hideFlags = HideFlags.None;
clone.SetActive(true);
Essentially, this tricks Unity Editor into believing that this object has always been there, and when we perform undo, the editor sets the newly created object’s hide flag to hide and don’t save, and deactivate it. When you exit Unity Editor, it won’t be saved to the scene.
Tl;dr, instead of deleting on undo, hide it.
Similar, on deletion, we can do:
GameObject clone = GetTileToDelete(...);
Undo.RecordObject(clone, "Delete Tile");
clone.hideFlags = HideFlags.HideAndDontSave;
clone.SetActive(false);
This will hide it, and it’s gone next time you start Unity.
This dramatically sped up my tool, by about 10x.
Unity should still fix this Undo system though! It’s really hard for tool developers