- Home /
How to remove undo from history (RevertAllInCurrentGroup?)
I'm trying to use RevertAllInCurrentGroup
to undo the last operation but it doesn't seem to work.
In some init code I have:
// 'component' is a MonoBehaviour-derived class. Here I create the default editor for the type
m_editor = Editor.CreateEditor(component);
In OnGUI
:
m_editor.OnInspectorGUI();
// ..'hasChanged' is set to true if the user changed some property
if (hasChanged)
{
// ..do some stuff using the new values on the object
Undo.RevertAllInCurrentGroup();
}
When I call RevertAllInCurrentGroup
I get InvalidOperationException: Operation is not valid due to the current state of the object
in some Stack
object in GUILayoutUtility.EndLayoutGroup
. I figured I shouldn't be doing the revert in OnGUI
, so I changed it to set a flag and then do it in Update
but that doesn't seem to make any difference.
Does anyone know how to use this revert function?
Answer by edwardrowe · Aug 23, 2016 at 03:15 PM
Unity should improve this error handling for sure...
So I'm not sure why the error occurs, but calling (the undocumented) GUIUtility.ExitGUI();
following the revert (from the OnInspectorGUI loop) fixed it for me. I noticed several of Unity's decompiled classes use that function, but I'm not sure exactly what it does.
Anyway, if anyone can shed light on what GUIUtility.ExitGUI does or why we need it, I'd love to hear.