Skip to content

Instantly share code, notes, and snippets.

@jimfleming
Created June 7, 2014 23:40
Show Gist options
  • Save jimfleming/37170cf50a6e443a78f5 to your computer and use it in GitHub Desktop.
Save jimfleming/37170cf50a6e443a78f5 to your computer and use it in GitHub Desktop.
Handles collapsing multiple undo operations. Definitely works in Unity 4.5. Haven't tested previous versions.
using UnityEditor;
using UnityEngine;
using System;
using System.Collections;
/*
Usage:
using (new UndoStack("Batch Changes")) {
// Make some changes using RecordObject or whatever...
}
*/
public class UndoStack : IDisposable {
private static GameObject _TempGO;
private static GameObject TempGO {
get {
if (_TempGO == null) {
_TempGO = new GameObject("{GameObject}");
_TempGO.hideFlags = HideFlags.HideAndDontSave;
}
return _TempGO;
}
}
public int GroupId { get; protected set; }
public UndoStack(string label) {
GroupId = Undo.GetCurrentGroup();
Undo.IncrementCurrentGroup();
Undo.RecordObject(TempGO, label);
TempGO.transform.localPosition += Vector3.one;
}
public void Dispose() {
Undo.CollapseUndoOperations(GroupId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment