Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Undo - Create Group

Discussion in 'Immediate Mode GUI (IMGUI)' started by mimminito, Mar 10, 2015.

  1. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Hi,

    Is there a way I can create a grouped undo operation? I am creating an asset which will modify properties on multiple GameObjects at once. I would like to be able to allow the user to undo those changes as a group change, not individually (as there couple be a lot!). How do I go about creating an undo "group" for the changes I know I am about to make? And then how do I perform that undo on that group only?

    Adam
     
  2. testure

    testure

    Joined:
    Jul 3, 2011
    Posts:
    81
    Yep, pretty easy to do actually:

    Code (CSharp):
    1. void ResetPositionOnSelected( )
    2. {
    3.     Undo.SetCurrentGroupName( "Zero out selected gameObjects" );
    4.     int group = Undo.GetCurrentGroup();
    5.  
    6.     Undo.RecordObjects( Selection.transforms, "transform selected objects" );
    7.  
    8.     foreach ( Transform t in Selection.transforms )
    9.     {
    10.         t.position = Vector3.zero;
    11.     }
    12.  
    13.     Undo.CollapseUndoOperations( group );
    14. }
     
    jack_walton, juana_nh and mimminito like this.
  3. mimminito

    mimminito

    Joined:
    Feb 10, 2010
    Posts:
    780
    Thanks for the help!