menu_bookDocumentation
Undo System - s&box Editor Documentation
Undo System
The undo system allows you to make editor actions reversible. Users can press Ctrl+Z to undo changes made by your tools.
Scope Based Undo
Create an undo scope for a group of changes:
CSHARP
using ( var scope = new UndoScope( "My Action" ) )
{
// Make changes here
go.WorldPosition = newPos;
// Changes are recorded and undoable
}GameObjects
Track GameObject changes:
CSHARP
var undoScope = SceneEditorSession.Active.UndoScope( "Move Object" )
.WithGameObjectChanges( selection, GameObjectUndoFlags.Properties );
using ( undoScope.Push() )
{
// Move selected objects
foreach ( var go in selection )
{
go.WorldPosition += Vector3.Up * 10f;
}
}Components
Capture component creation and destruction:
CSHARP
// Capture component creation
var scope = new UndoScope( "Add Component" )
.WithComponentCreations();
// Capture component destruction
var scope = new UndoScope( "Remove Component" )
.WithComponentDestructions();Selections
Track selection changes:
CSHARP
var scope = new UndoScope( "Select Objects" )
.WithSelectionChanges();Chaining
Chain multiple undo scopes together:
CSHARP
var scope1 = new UndoScope( "First Action" ).Push();
var scope2 = new UndoScope( "Second Action" ).Push();Multi-Frame Actions
For actions that span multiple frames:
CSHARP
var scope = new UndoScope( "Drag Operation" )
.WithGameObjectChanges( selection, GameObjectUndoFlags.Properties );
// Start the scope
using ( scope.Push() )
{
// Do work over multiple frames
while ( dragging )
{
UpdatePositions();
yield return null;
}
}Always provide clear action names so users know what they're undoing.
Was this helpful?