【UnityEditor】Undo撤銷編輯器下的操作,如撤銷建立/銷燬的物體
阿新 • • 發佈:2020-12-17
技術標籤:工具
編輯器下的一些操作預設是不可撤銷的,如建立、銷燬物體,使用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(); } }