我試圖使用RevertAllInCurrentGroup
撤消上一個操作,但它似乎不起作用。其實我真正想做的只是從歷史中刪除撤消;我不在乎撤消是否適用。如何從歷史記錄中刪除撤銷(RevertAllInCurrentGroup?)
在一些初始化代碼,我有:
// 'component' is a MonoBehaviour-derived class. Here I create the default editor for the component
m_editor = Editor.CreateEditor(component);
然後在OnGUI
在我EditorWindow
:
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, which includes sending a message to a server
Undo.RevertAllInCurrentGroup();
}
當我打電話RevertAllInCurrentGroup
我得到InvalidOperationException: Operation is not valid due to the current state of the object
在一些Stack
對象GUILayoutUtility.EndLayoutGroup
。我想也許我不應該在OnGUI
做恢復,所以我改變它設置一個標誌,並在Update
做恢復,但這並沒有什麼區別(除了我明顯不再有例外)。
有誰知道如何使用這個函數,或者如果有其他方法可以撤消Editor
實例所應用的最後一個操作?我試過使用Undo.RegisterCompleteObjectUndo
和Undo.ClearUndo
,但他們似乎也沒有做任何事情(撤消操作仍然出現在撤消堆棧中)。
爲了便於說明,我使用編輯器中的組件根據從服務器接收到的消息動態創建GameObjects
(這是一個正在運行的Unity遊戲,可能在編輯器內部或外部 - 這是一個實時更新系統) 。然後,我允許編輯這些組件,並將更改後的組件發送回服務器。我正在渲染我自己的檢查器UI,並且我想爲組件使用內置的Editor
實例(例如,如果組件中有Camera
,則將使用內置的CameraEditor
)。
唯一的問題是,使用內置編輯器會將撤消操作添加到堆棧,但我真的不在意這些撤消操作,因爲它們適用的GameObject
只是一個臨時佔位符GameObject
,它是每當我收到來自服務器的新消息時,都會不斷更新每秒的coulpe。
啊是啊,你說得對,我可以用它!我的意思是,雖然我想撤消我正在使用的編輯器實例所應用的最後一個操作(或者撤消/刪除當前正在編輯的對象的所有撤消歷史記錄)。無論如何,我會保持這種情況,以防其他人有任何其他建議。 – Sam
實際上這並不好,因爲它不會從撤消堆棧中刪除操作。我仍然可以「重做」手術。我需要將它從撤消堆棧中完全移除。 – Sam