Version: 2023.1

Undo.GetCurrentGroup

切换到手册
public static int GetCurrentGroup ();

返回

int The index of the current undo group.

描述

Unity 根据当前的组索引自动对撤销操作进行分组。

按下鼠标、单击菜单项和进行其他操作时,当前的组索引会自动增加。

See Also: Undo.RevertAllDownToGroup, Undo.CollapseUndoOperations.

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);
    }
}