• 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
3
Question by kLy · Aug 01, 2013 at 02:03 PM · guipropertydrawer

How to repaint from a Property Drawer?

From custom editors when you change something that you know affects the layout / content of the GUI you can force a redraw using Repaint()

How would you do this from a Property Drawer? It seems like there isn't a way to reference the Editor that contains the Property Drawer to call the Repaint.

Comment
Add comment · Show 1
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 yoyo · Sep 04, 2013 at 05:59 AM 1
Share

Not sure, but you could try setting the property to its current value, to see a value "change" forces a redraw. For example, property.floatValue = property.floatValue;

7 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by rnhfjkui · Dec 17, 2018 at 09:28 AM

Immediate Repaint of the current inspector without reflection or anything funny, works and tested:

 public static void RepaintInspector(SerializedObject BaseObject)
     {
         foreach (var item in ActiveEditorTracker.sharedTracker.activeEditors)
             if (item.serializedObject == BaseObject)
             { item.Repaint(); return; }
     }

how does it work? It utilizes an undocumented unity class called 'ActiveEditorTracker', Very USEFUL class that shows you all the Active editors in the inspector. to find the Actual editor the property drawer is a part of we iterate until we find the editor with the SerializedObject the same as our property, we then repaint it.

This class is like magic, I have been doing editor improvements for the last month and a half and it has been a godsend. (you can use it to draw OnSceneGUI from a property drawer as well with some tricks) Would love to see it actually documented.

I know this is an extremely old question but if someone comes across this and is looking for the real solution this is it.

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 hogwash · Jul 25, 2014 at 05:55 AM

EditorUtility.SetDirty( property.serializedObject.targetObject ); // Repaint

Comment
Add comment · Show 3 · 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 Jeff-Kesselman · Aug 03, 2014 at 04:13 AM 1
Share

Tried it. Doesnt work.

avatar image gsmetzer · May 20, 2015 at 04:42 PM 0
Share

Worked great for me!

avatar image luizero00 · Jun 05, 2018 at 09:17 PM 0
Share

It works, thanks!

avatar image
1

Answer by Bunny83 · Dec 10, 2014 at 12:43 AM

All the Repaint method in an editor actually does, is calling a static method in the InspectorWindow class. Unfortunately this class is an internal class. So you basically have two options:

  • Create a temporary instance of an Editor to get your hands on the Repaint method and use DestroyImmediate right after it.

  • Use reflection to call the static method manually.

I would go for the second solution:

 public static class InspectorHelpers
 {
     private static System.Reflection.MethodInfo m_RepaintInspectors = null;
     public static void RepaintAllInspectors()
     {
         if (m_RepaintInspectors == null)
         {
             var inspWin = typeof(EditorApplication).Assembly.GetType("UnityEditor.InspectorWindow");
             m_RepaintInspectors = inspWin.GetMethod("RepaintAllInspectors", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic );
         }
         m_RepaintInspectors.Invoke(null, null);
     }
 }

Haven't tested the class but it should work this way ;)

Comment
Add comment · Show 2 · 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 ShawnFeatherly · Sep 04, 2019 at 10:32 PM 1
Share

GetType() required the full name for me. Otherwise works great! var inspWin = typeof(UnityEditor.EditorApplication).Assembly.GetType("UnityEditor.InspectorWindow");

avatar image Bunny83 ShawnFeatherly · Sep 04, 2019 at 10:49 PM 0
Share

Yes, you're right. Of course we have to use the full class name (including namespace) since there are no "using" statements for reflection ^^. I changed my examle. Now that I see this post again, you could even get a slightly better preformance by creating a delegate out of the methodInfo

avatar image
0

Answer by RichardAchahboun · Dec 10, 2014 at 02:20 AM

Ok, I know I'm late to the party but this worked for me and it might help others:

I check if the object has changed and then inside my check I apply those changed properties and update. I've not included the dirty checking as that's likely to change a lot between implementations anyway.

 serializedObject.ApplyModifiedProperties();
 serializedObject.Update();

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 johnwbyrd · Dec 24, 2014 at 07:26 AM

There may be a way to reference the Editor in question, but I haven't found one. What I have done instead is to ask the object that the PropertyDrawer represents, whether it wants to be repainted from your CustomEditor class. I suspect there is a more elegant way, but this is what works for me.

     [CanEditMultipleObjects]
     [CustomEditor(typeof(YourClass)) ]
     public class YourCustomEditor : Editor
     {
             public override void OnInspectorGUI ()
             {
                     serializedObject.Update();
                     base.OnInspectorGUI ();
                     serializedObject.ApplyModifiedProperties();
                     YourClass lite = (YourClass) target;
                     if ( lite.ShouldRepaint() )
                     {
                         Repaint ();
                     }
                     
             }
     }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Constantly Update OnGUI while game is not running 0 Answers

How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer? 2 Answers

(Solution) - Can't use GUILayout stuff in PropertyDrawer.OnGUI? 2 Answers

new Line in a CustomPropertyDrawer 1 Answer

Drawing Order of DecoratorDrawer doesn't work? 1 Answer


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