Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Notes
- Add this to the end of a custom editor.
- Make sure there is a member variable for thisTarget..
- Make sure target code is in OnEnable()
- Make sure CheckUndo(); is at the top of the OnInspectorGUI() method
- Make sure you have: guiChange = true; as part of a button.
- Make sure you have if (Gui.Changed) {guiChanged = true;} at the end of OnInspectorGUI;
- Paste CheckUndo method at the end of the custom editor script.
- */
- private bool listeningForGuiChanges;
- private bool guiChanged;
- public TargetType thisTarget;
- void OnEnable () {
- thisTarget = target as TargetType;
- // The rest of the OnEnable code...
- }
- public override void OnInspectorGUI () {
- CheckUndo();
- // The rest of the EditorGUI code...
- if (GUILayout.Button( "SomeButton" ) ) {
- guiChanged = true;
- DoSomething();
- }
- if ( GUI.changed ) {
- guiChanged = true;
- }
- }
- private void CheckUndo() {
- // Modified from code found here:
- // http://answers.unity3d.com/questions/50954/how-to-correctly-register-undos-in-custom-inspecto.html
- Event e = Event.current;
- if (e.type == EventType.MouseDown && e.button == 0 || e.type == EventType.KeyUp && (e.keyCode == KeyCode.Tab)) {
- // When the LMB is pressed or the TAB key is released,
- // store a snapshot, but don't register it as an undo
- // (so that if nothing changes we avoid storing a useless undo)
- // Debug.Log("PREPARE UNDO SNAPSHOT");
- Undo.SetSnapshotTarget(thisTarget, "Message Displays");
- Undo.CreateSnapshot();
- Undo.ClearSnapshotTarget();
- listeningForGuiChanges = true;
- guiChanged = false;
- }
- if (listeningForGuiChanges && guiChanged) {
- // Some GUI value changed after pressing the mouse.
- // Register the previous snapshot as a valid undo.
- // Debug.Log("REGISTER UNDO");
- Undo.SetSnapshotTarget(thisTarget, "Message Displays");
- Undo.RegisterSnapshot();
- Undo.ClearSnapshotTarget();
- listeningForGuiChanges = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement