Unity的Undo撤销操作也是可以自定义的,当我们将自己的操作组册到Undo事件中,那么,我们就可以按照自己的方式撤销操作了。
目录
- Undo操作的简单测试
- Undo操作的分组折叠
- 对选中物体操作并注册Undo练习
- 将Undo操作分组撤销到指定位置
Undo操作的简单测试
下面是一些撤销测试,非常简单,直接看代码
using UnityEngine;using UnityEditor;public class UndoTest { [MenuItem("Undo/RecordObject")] static void RecordObject () { Transform transform = Selection.activeTransform; Undo.RecordObject (transform, "Pos"); transform.position = new Vector3 (0, 0, 0); } [MenuItem("Undo/AddComponent")] static void AddComponent () { GameObject go = Selection.activeGameObject; Rigidbody rigidbody = Undo.AddComponent<Rigidbody> (go); } [MenuItem("Undo/DestroyObjectImmediate")] static void DestroyObjectImmediate () { GameObject go = Selection.activeGameObject; Undo.DestroyObjectImmediate (go); } [MenuItem("Undo/SetTransformParent")] static void SetTransformParent () { Transform root = Camera.main.transform; Transform transform = Selection.activeTransform; Undo.SetTransformParent (transform, root, "Main Cameta"); } [MenuItem("Undo/RevertAllInCurrentGroup")] static void RevertAllInCurrentGroup () { GameObject ticket = new GameObject ("Ticket"); Undo.RegisterCreatedObjectUndo (ticket, "UndoCreate"); int number = ticket.GetInstanceID (); int winningNumber = 0; Debug.Log (ticket.GetInstanceID()); if (ticket.GetInstanceID ().ToString().Contains(winningNumber.ToString())) { Undo.RevertAllInCurrentGroup (); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
Undo操作的分组折叠
撤销操作可以将一系列操作分为一组,这样一次撤销就可以撤销这一组的操作了
using UnityEngine;using UnityEditor;public class UndoWindow : EditorWindow{ int groupID = 0; [MenuItem ("Window/UndoWindow")] static void Open () { GetWindow<UndoWindow> (); } void OnEnable () { groupID = Undo.GetCurrentGroup (); } void OnDisable () { Undo.CollapseUndoOperations (groupID); } void OnGUI () { if (GUILayout.Button ("Cube 生成")) { var cube = GameObject.CreatePrimitive (PrimitiveType.Cube); Undo.RegisterCreatedObjectUndo (cube, "Create Cube"); } if (GUILayout.Button ("Plane 生成")) { var plane = GameObject.CreatePrimitive (PrimitiveType.Plane); Undo.RegisterCreatedObjectUndo (plane, "Create Plane"); } if (GUILayout.Button ("Cylinder 生成")) { var cylinder = GameObject.CreatePrimitive (PrimitiveType.Cylinder); Undo.RegisterCreatedObjectUndo (cylinder, "Create Cylinder"); } if (GUILayout.Button("折叠撤销操作")) { Undo.CollapseUndoOperations (groupID); } if (GUILayout.Button("撤销")) { Undo.PerformUndo (); } if (GUILayout.Button("取消撤销")) { Undo.PerformRedo (); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
对选中物体操作,并注册Undo练习
将自己编写的一些操作加入到Undo中
using UnityEngine;using UnityEditor;public class Example{ [MenuItem("Example/Create Cube")] static void CreateCube () { var cube=GameObject.CreatePrimitive (PrimitiveType.Cube); Undo.RegisterCreatedObjectUndo (cube, "Create Cube"); } [MenuItem("Example/Random Rotate")] static void RandomRotate () { var transform = Selection.activeTransform; if (transform) { Undo.RecordObject (transform, "Rotate " + transform.name); transform.rotation = Random.rotation; } } [MenuItem("Example/Random Rotate2")] static void RandomRotate2 () { var transform = Selection.activeTransform; if (transform) { Undo.willFlushUndoRecord += () => Debug.Log ("flush"); Undo.postprocessModifications += (modifications) => { Debug.Log ("modifications"); return modifications; }; Undo.RecordObject (transform, "Rotate 2" + transform.name); Debug.Log ("recorded"); transform.rotation = Random.rotation; Debug.Log("changed"); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
将Undo操作分组,撤销到指定位置
Undo的每一步操作都有它的ID,纪录ID就可以实现特殊的Undo操作
using UnityEngine;using UnityEditor;public class ExampleWindow : EditorWindow{ [MenuItem("Window/ExampleWindow")] static void Open () { GetWindow<ExampleWindow> (); } GameObject go; int group1 = 0; int group2 = 0; int group3 = 0; void OnEnable () { go = Camera.main.gameObject; } void OnGUI () { if (GUILayout.Button("给摄像机添加组件")) { group1 = Undo.GetCurrentGroup(); Undo.AddComponent<Rigidbody> (go); Undo.IncrementCurrentGroup(); group2 = Undo.GetCurrentGroup(); Undo.AddComponent<BoxCollider> (go); Undo.IncrementCurrentGroup(); group3 = Undo.GetCurrentGroup(); Undo.AddComponent<ConstantForce>(go); Debug.Log (group1+"--"+group2+"--"+group3); } if (GUILayout.Button("取消到group2状态")) { Undo.RevertAllDownToGroup(group2); EditorGUIUtility.ExitGUI(); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
本文工程:http://download.csdn.net/detail/warrenmondeville/9708468
本文链接:http://blog.csdn.net/WarrenMondeville/article/details/53577379