编辑器下的一些操作默认是不可撤销的,如创建、销毁物体,使用Undo可以将某个操作标记为可撤销操作,Ctrl+Z即可撤销

实例:
using UnityEngine;
using UnityEditor;
public class TestUndoEditorWindow
{
[InitializeOnLoadMethod]
public static void Init()
{
SceneView.duringSceneGui += DuringSceneGui;
}
private static void DuringSceneGui(SceneView sceneView)
{
Handles.BeginGUI();
if(GUI.Button(new Rect(0,10,100,20),"创建单个Cube"))
{
//手动对可撤销操作分组
Undo.IncrementCurrentGroup();
//创建Cube
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//销毁Cube
//Undo.DestroyObjectImmediate(cube);
//标记可撤销操作
Undo.RegisterCreatedObjectUndo(cube,"创建单个Cube : " + cube.name);
}
if(GUI.Button(new Rect(0,35,100,20),"创建多个Cube"))
{
Undo.IncrementCurrentGroup();
for(int i = 0; i < 5; i++)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(i * 1.5f,0,0);
Undo.RegisterCreatedObjectUndo(cube,"创建多个Cube : " + cube.name);
}
}
Handles.EndGUI();
}
}
本文来自博客园,作者:萧然CS,转载请注明原文链接:https://www.cnblogs.com/z-c-s/p/15112854.html
浙公网安备 33010602011771号