Version: 2023.2

Undo.CollapseUndoOperations

切换到手册
public static void CollapseUndoOperations (int groupIndex);

参数

groupIndex The group index to collapse undo operations to.

描述

将组索引之下的所有撤销操作折叠到一个步骤中。

The current group index automatically increases on mouse down, by clicking on menu items, and other operations. This method collapses all undo operations with group indices above the provided group index to that group index. This is used by the color picker to combine all color modifications into a single step once the color picker is closed, while they are seperate operations when the color picker is still open.

using UnityEditor;
using UnityEngine;

public class ResetPositionForSelectedGameObjectsExample : MonoBehaviour
{
    [MenuItem("MyMenu/Reset Positions of Selected GameObjects")]
    static void ResetPositionForSelectedGameObjects()
    {
        Undo.SetCurrentGroupName("Zero out selected gameObjects");
        int group = Undo.GetCurrentGroup();

        Undo.RecordObjects(Selection.transforms, "transform selected objects");

        foreach (Transform t in Selection.transforms)
        {
            t.position = Vector3.zero;
        }

        Undo.CollapseUndoOperations(group);
    }
}