x


Repaint on Undo

I need to repaint an EditorWindow when the user performs undo/redo.

But I don't see any callbacks or events associated with undo/redo...

How can I do this?

more ▼

asked Nov 06 '10 at 03:57 PM

Alex Chouls gravatar image

Alex Chouls
428 11 15 25

How do I add a bounty to this question?! Poked all around the UI, am I missing something obvious?!

Nov 22 '10 at 01:51 AM Alex Chouls

Never-mind, found the answer. Guess I need a minimum of 75 reputation to post a bounty... :(

Nov 22 '10 at 01:54 AM Alex Chouls
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

This seems to work:

if (Event.current.type == EventType.ValidateCommand)
{
    switch (Event.current.commandName)
    {
        case "UndoRedoPerformed":

            // repaint etc.
            break;
    }
}

Add this to OnGUI(). I have it in a switch since I'm reacting to other events as well... You can debug log the commandName to get the name of the command you're interested in (that's how I found UndoRedoPerformed).

EDIT: This method turns out to be unreliable since only the active window gets the event. IOW, if the user performs undo/redo with another window active, my EditorWindow never gets the event.

It's sort of okay(-ish) for repaint since the editor window will repaint anyway when it gets focus again, but unfortunately I need to do some other stuff too (re-initialize some data). So if you need to intercept undo/redo reliably, this doesn't work.

Internally it seems that unity uses an Undo callback, but it's not exposed for tool developers :(

more ▼

answered Nov 19 '10 at 09:59 PM

Alex Chouls gravatar image

Alex Chouls
428 11 15 25

(comments are locked)
10|3000 characters needed characters left

I'll see what I can do about making EditorApplication.undoRedoPerformed public. If you want to go cowboy here, you can try it out by registering your own callback on that event trough reflection.

I would never suggest you do this for runtime code, but for editorcode there is less risk. Please note that the reason we keep certain things internal are usually:

  • it might not work
  • we are planning to change how it works (read: your code has a high change of breaking in future unity updates, even dotreleases)

If you can live with those two constraints, give this a shot.

See http://stackoverflow.com/questions/3120422/how-to-attach-event-handler-to-an-event-using-reflection for inspiration on how to register for events using reflection.

PPS2: on every domain reload (when you press play), you will need to register your eventhandler again, events are not serialized by Unity.

more ▼

answered Nov 24 '10 at 11:37 AM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

Thanks Lucas! I'll try attaching my own callback...

Nov 24 '10 at 02:18 PM Alex Chouls
(comments are locked)
10|3000 characters needed characters left

I didn't manage to grab the events from EditorApplication but I found out the undoRedoPerformed field is still there only it is private. So I set it using reflection and now it's working even when the window is not focused. I call this in the editor constructor:

FieldInfo undoCallback = typeof(EditorApplication).GetField("undoRedoPerformed", BindingFlags.NonPublic | BindingFlags.Static);
undoCallback.SetValue(null, (EditorApplication.CallbackFunction)OnUndoRedo);

Note that I'm working with Editor inside the inspector instead of a separate EditorWindow but I think it should work there as well.

more ▼

answered Jun 15 '11 at 08:25 AM

hardwire gravatar image

hardwire
106 1 1 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x171
x29
x11
x5

asked: Nov 06 '10 at 03:57 PM

Seen: 1467 times

Last Updated: Jun 16 '11 at 11:38 AM