Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by Alex-Chouls · Nov 06, 2010 at 03:57 PM · editorwindowundorepaint

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alex-Chouls · Nov 22, 2010 at 01:51 AM 0
Share

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

avatar image Alex-Chouls · Nov 22, 2010 at 01:54 AM 0
Share

Never-$$anonymous$$d, found the answer. Guess I need a $$anonymous$$imum of 75 reputation to post a bounty... :(

5 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Alex-Chouls · Nov 19, 2010 at 09:59 PM

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 :(

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
5
Best Answer

Answer by Lucas Meijer 1 · Nov 24, 2010 at 11:37 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alex-Chouls · Nov 24, 2010 at 02:18 PM 0
Share

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

avatar image
3

Answer by guavaman · Jun 24, 2014 at 08:41 PM

This has now been replaced by Undo.undoRedoPerformed as of Unity 4.3

Usage:

 Undo.undoRedoPerformed += OnUndoRedo; // subscribe to the event
 
 void OnUndoRedo() {
     // some code here
 }

According to Lucas Meijer in a message elsewhere on this page:

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by hardwire · Jun 15, 2011 at 08:25 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

Answer by sirbrialliance · Dec 20, 2013 at 10:18 PM

You'll get a validation event when an undo is performed, you can hook into that event:

 public void OnSceneGUI() {
     if (Event.current.commandName == "UndoRedoPerformed") {
         OnUndoRedo();
         return;
     }
     //...
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you undo operations from an EditorWindow? 1 Answer

How to force Unity to Repaint an EditorWindow? 1 Answer

undo and redo 0 Answers

Determine difference between Undo and Redo operations 1 Answer

Undo problems with a Custom EditorWindow 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges