*Edit: Solution below in edited code. ////////////////////////////////////////////////////
I've been trying to incorporate undo functionality for my custom editor script for a few hours now, with no success.
I've tried all functions associated with the "undo" class, but I just cannot get it to work.
Can someone please explain the proper way to get a slider value tied into the undo system?
The (fixed) code I now have:
I followed the links to get to this code. I tested it and it works, however for some strange reason I get imprecision for the value when I try to undo. 0.1 might become 0.123 et.c. Also, if you select the text box with the value, then drag the slider, you will be creating snapshots for every single frame for some reason. I tried to make the code so it only create a snapshot when the mouse button is released but I couldn't get any input code working (Neither Input.GetMouseButtonXxx nor Event.current.button worked for me)! If you know a way to handle this case you might get a better solution.
Edit: You might get this to work if you set
Anyhow, there's 5 changes to your code. See comments in code, they are also numbered for ease.
Worth noting is that it seems there is a slight delay from when you press CTRL + Z (Undo shortcut) to the actual value is undone. This doesn't seem to be the case if the text box is selected when you do this. I don't know why this happens. Maybe I am overlooking something or maybe it's some mechanic of UnityEditor that does this.
Edit: After further investigation it seems that OnGUI isn't called continously. It is only called when the user interacts with the GUI. This causes the lag inside the editor window since it isn't repainted. I fixed this by adding Repaint to key modifiers although it might not be the best way to go about. It solved this particlular case however and I've updated the code.
And the code follows: