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
4
Question by drastick · Oct 16, 2012 at 06:16 AM · inspectorrefreshoninspectorguiredraw

How do you force a custom inspector to redraw?

I have a custom editor inspector that I have labels that report data that gets sycn via a custom button on the same inspector, this starts up WWW class to get the data. Once the data syncs and is posted into the value that the lable consumes the custom inspector. However, it does not refresh the lable value unless I click on the inspector window again. Is it possible to force the inspector to redraw at the time the valus changes?

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

7 Replies

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

Answer by JanWosnitza · Oct 16, 2012 at 04:36 PM

Call 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 drastick · Oct 17, 2012 at 06:54 AM 0
Share

Good but Editor.Repaint() is only for scripts using UnityEditor

I want to have game code (non editor scripts) while running to trigger this event repaint event so can watch the gameplay values refresh in editor. Does the editor have an update that can check a value to see if Editor repaint is needed? It seems I could do this with an EditorWindow as it has update functions but what about extending the editor class?

avatar image
23

Answer by ________ · Aug 06, 2013 at 11:24 AM

I know this is old but I came looking for an answer to this and found that calling:

 EditorUtility.SetDirty( target );

will refresh the editor every frame.

 void OnInspectorGUI()
 {
    //Logic
    EditorUtility.SetDirty( target );
 }
Comment
Add comment · Show 5 · 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 TimK_personal · Jun 18, 2015 at 07:12 PM 0
Share

I don't understand how this could work. If OnInspectorGUI were being called, wouldn't it refresh already? This would make more sense to me if you had an Update method in which you called SetDirty.

Anyone tried this? DOES it actually work?

EDIT: Okay, so I'm an idiot. It was easy enough to try it, and it DOES work, although I'm kinda baffled as to how. I guess maybe the editor base class holds a deep copy of its target?

avatar image charmi212121 · Feb 10, 2016 at 09:36 AM 0
Share

@ It works great Thanks!!

avatar image Jon_Brant · Mar 18, 2019 at 03:44 AM 0
Share

Still works like a charm in 2018.2.5f1, thank you!

avatar image Xarbrough · Nov 23, 2019 at 12:39 PM 5
Share

Don't use this to redraw an inspector. SetDirty is intended to mark an asset as having changes that need to be saved by Unity. In the past it could also be used to mark GameObjects dirty, which would dirty the scene, but should no longer be used for this purpose. Incidentally this will also force a repaint, but this is only a side-effect and should not be misused like this. Call Repaint() on the editor instance ins$$anonymous$$d. Or, if you don't have access to the instance, use UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

avatar image lerichard Xarbrough · Feb 04 at 06:30 PM 0
Share

Understandable, both EditorUtility.SetDirty(target); and Repaint(); worked well for me, but i will use Repaint as you said.

avatar image
6

Answer by yoyo · Dec 04, 2012 at 06:04 AM

Here's a technique I've used for getting game code to call editor code. Rather than calling directly (which you can't do), you can create an event delegate in the game code to which the editor code attaches itself.

So for your component that wants to tell the editor code to repaint itself, add something like this:

 public class MyComponent : MonoBehaviour
 {
     // Declare the method signature of the delegate to call.
     // For a void method with no parameters you could just use System.Action.
     public delegate void RepaintAction();
 
     // Declare the event to which editor code will hook itself.
     public event RepaintAction WantRepaint;
 
     // This private method will invoke the event and thus the attached editor code.
     private void Repaint()
     {
         // If no handlers are attached to the event then don't invoke it.
         if (WantRepaint != null)
         {
             WantRepaint();
         }
     }

     ...
 }

Then in your custom inspector, have it attach its Repaint method to this event:

 [CustomEditor(typeof(MyComponent))]
 public class MyComponentEditor : Editor
 {
     // I like having the typecast to my component in one place, instead of everywhere I refer to target.
     private MyComponent Target
     {
         get { return (MyComponent)target; }
     }
 
     // When you enable the inspector, attach to the game code that wants to call it.
     void OnEnable()
     {
         Target.WantRepaint += this.Repaint;
     }
 
     // And then detach on disable.
     void OnDisable()
     {
         Target.WantRepaint -= this.Repaint;
     }
 
     ...
 }

And now whenever your game code (in MyComponent) calls Repaint, the call will get dispatched via the event hook-up into the inspector, and a repaint will happen. So you can call Repaint as often or as little as you like.

A similar approach can be used any time you want activity in game code to trigger results in editor code. (I usually wrap all the game code side of this in #if UNITY_EDITOR, so it gets compiled away in a build.)

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
6

Answer by robinryf · Aug 08, 2016 at 07:13 PM

This was probably not available when the question was asked. But this is my recommended way:

 [CustomEditor(typeof(MyType))]
 public class MyTypeInspector : Editor
 {
     public override bool RequiresConstantRepaint()
     {
         return true;
     }
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         // Draw stuff
     }
 }
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 Amitloaf · Nov 25, 2018 at 06:07 PM 0
Share

This is what I was looking for. Even

 void Update() 
 {
     Repaint();
 }

didn't work for me. This did. Thank you!

avatar image
1

Answer by Demigiant · Oct 17, 2012 at 05:59 PM

If you need to refresh an inspector Window while Unity is playing, you can simply change the value of a serialized field in its target, and the Inspector will repaint automatically. See my self-answered question here: http://answers.unity3d.com/questions/330518/refreshing-custom-inspector-window-while-playing.html

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
  • 1
  • 2
  • ›

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom editor desperation: instance of custom class 1 Answer

manually refresh scripts in inspector 1 Answer

Custom inspector resets values when refreshing 1 Answer

OnInspectorGUI not working 1 Answer

Is there a way to draw on the scene view from an asset being inspected by the Inspector? 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