This static class manages tilemap updates during editor undo and redo operations. It subscribes to the editor's undo/redo history events and, upon restoration, iterates over all tilemap components in the current scene to invalidate their data.
using Editor;
using Sandbox.Helpers;
using Saandy.Tilemapper;
namespace Saandy.Editor.Tilemapper;
/// <summary>Undo can restore serialized collections without invoking component validation.</summary>
public static class TilemapUndoRefresh
{
private static SceneEditorSession Session;
private static bool RefreshPending;
[EditorEvent.Frame]
public static void Update()
{
FlushPending();
var active = SceneEditorSession.Active;
if ( Session == active ) { return; }
if ( Session != null )
{
Session.UndoSystem.OnUndo -= OnHistoryRestored;
Session.UndoSystem.OnRedo -= OnHistoryRestored;
}
Session = active;
if ( Session != null )
{
Session.UndoSystem.OnUndo += OnHistoryRestored;
Session.UndoSystem.OnRedo += OnHistoryRestored;
}
}
private static void OnHistoryRestored( UndoSystem.Entry entry )
{
// Defer until restoration has completed, regardless of notification ordering.
RefreshPending = true;
}
public static void FlushPending()
{
if ( !RefreshPending ) { return; }
RefreshPending = false;
var scene = Session?.Scene;
if ( scene == null || !scene.IsValid || !scene.IsEditor ) { return; }
foreach ( var tilemap in scene.GetAllComponents<TileMap>() )
{
tilemap.InvalidateTileData();
}
}
}