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
10
Question by jbourrie · Dec 18, 2010 at 11:46 PM · editoreditorwindowcustomrepaintredraw

Repainting an EditorWindow when it's not the current focus?

I am currently working on a custom EditorWindow for a particular type of GameObject. When I select different objects in the Hierarchy window, the custom window is updated to edit this new object.

This all works fine, except the custom window doesn't redraw until I mouse-over it again. I've tried manually calling Repaint() on the window (didn't work), looked through the documentation and didn't see anything, so now I'm asking here.

How can I repaint a custom EditorWindow when it's not the active focus?

Thanks in advance.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
27

Answer by spolglase · Jan 17, 2012 at 09:11 PM

I've found that adding Repaint() to the Update function of your EditorWindow does the trick. This also has the added effect of repainting the editor window way more often than it does normally which I also found handy in my case.

 public void Update()
 {
     // This is necessary to make the framerate normal for the editor window.
     Repaint();
 }

I suppose if you want your Repaint to be less intrusive you could put it in EditorWindow.OnInspectorUpdate() because it only gets called 10 times per second. That would probably be better for performance.

 public void OnInspectorUpdate()
 {
     // This will only get called 10 times per second.
     Repaint();
 }
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 nickfourtimes · Apr 08, 2015 at 09:16 PM 1
Share

Years later, but this was still a life-saver. Thanks so much!

avatar image
3

Answer by jbourrie · Dec 18, 2010 at 11:52 PM

...it always seems to work this way, doesn't it? I ask the question after hunting through all the docs, then the minute I post I find the answer :)

For anyone looking at this in the future, EditorWindow.OnGUI() appears to only get called when the window is focused. Moving the Repaint() to EditorWindow.OnSelectionChanged() worked.

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
3

Answer by fadden · Oct 22, 2015 at 10:11 PM

I have an EditorWindow for a particular type of object. I found that it generally redrew when needed if I did this:

 // Called when the item selected in the inspector changes.  This only fires when our
 // editor window is visible.
 void OnSelectionChange() {
     m_selectedObj = GetSelection();
     Repaint();
 }
 
 // OnSelectionChange() only fires when the window is visible.  We need to grab it
 // when the window comes back into view.  I don't think OnFocus is quite right, since
 // in theory the window could become visible without being focused, but in practice
 // it works (and there's nothing better).
 //
 // Because the window is returning to visibility, Repaint() is usually redundant.
 // However, if we make changes in Visual Studio and then return to Unity, we do get
 // an OnFocus without a subsequent repaint.
 void OnFocus() {
     m_selectedObj = GetSelection();
     Repaint();
 }

The GetSelection() method is using Selection.activeGameObject() to grab whatever is currently selected, setting it to null if it doesn't contain my target class as a component.

This works well in terms of being able to move around and select different objects, and it even refreshes if I update the editor code and return to Unity. It fails in one significant aspect though: if you change values in the inspector window, the editor window doesn't get notified, and doesn't redraw.

So it seems that calling Repaint() from OnInspectorUpdate() is necessary, which is rather annoying since it means the window is redrawing at 10x per second even when you're not doing anything. I don't see a way around this though.

(Edit: If you check for changes, and only call Repaint() when the object has been updated, you can avoid the full cost of the repaint. And I suppose the underlying object could have an event that the EditorWindow could subscribe to when the selection changes, and then a CustomEditor could fire that event whenever OnInspectorGUI runs...)

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

2 People are following this question.

avatar image avatar image

Related Questions

When is the best time to call Repaint(); in an editor window 1 Answer

How to force Unity to Repaint an EditorWindow? 1 Answer

How to combine two buttons in one. Editor window 2 Answers

EditorGUILayout.TextArea text goes out of bounds 0 Answers

Unity editor and inspector header 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