• 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
2
Question by codestage · May 16, 2016 at 05:01 PM · editorinspector

How to manually refresh Inspector from Editor code?

Hey guys!

I have an object with some components on it. I wish to fold off (collapse) all component's properties on selected object.

I have a button in the EditorWindow which runs this code:

 Component[] components = go.GetComponents<Component>();
 foreach (Component component in components)
 {
     SerializedObject so = new SerializedObject(component);
     SerializedProperty iterator = so.GetIterator();
     
     do
     {
         iterator.isExpanded = false;
     } 
     while (iterator.NextVisible(true));
     
     so.ApplyModifiedProperties();
     EditorUtility.SetDirty(component);
 }
 EditorUtility.SetDirty(go);

go - is a selected object.

Code does job I need. But for the some reason I can see changes only if I deselect and re-select that object again.

Both

 AssetDatabase.Refresh();

and

 InternalEditorUtility.RepaintAllViews()

doesn't help here.

Am I missing something obvious (I bet I do)?

Thanks!

Comment
Add comment · Show 8
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 Glurth · May 16, 2016 at 10:32 PM 0
Share

"But for the some reason I can see changes only if I deselect and re-select that object again." This symptom makes me want to see the code where you set the gameObject reference go.

avatar image codestage Glurth · May 17, 2016 at 12:21 PM 0
Share

Hey, @Glurth, thanks for your comment. That reference comes from the Selection (it's just a currently selected GameObject).

avatar image Glurth · May 16, 2016 at 10:38 PM 0
Share

Also, Editor class itself has http://docs.unity3d.com/ScriptReference/Editor.Repaint.html did you try: this.Repaint() yet?

avatar image codestage Glurth · May 17, 2016 at 12:22 PM 0
Share

I'm running that code not from the Editor. I have EditorWindow where I have a button which calls that code %)

avatar image Glurth codestage · May 17, 2016 at 02:39 PM 0
Share

That link is for the Editor Class, not the "unity editor" in general. That is the class you derive from when creating an editor inspector. The EditorWindow class actually has a "repaint" function too.

Show more comments

3 Replies

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

Answer by Leslie-Young · Apr 04, 2017 at 04:12 PM

I know, old question but I was just looking for this, having forgotten that I did do it in one of my old projects before. So I'll post a solution in case someone else happens on this.

This assumes you want to force a repaint on a custom inspector.

 public static void RepaintInspector(System.Type t)
 {
     Editor[] ed = (Editor[])Resources.FindObjectsOfTypeAll<Editor>();
     for (int i = 0; i < ed.Length; i++)
     {
         if (ed[i].GetType() == t)
         {
             ed[i].Repaint();
             return;
         }
     }
 }

and to use

 RepaintInspector(typeof(SomeTypeInspector));
 
 where
 
 [CustomEditor(typeof(SomeType))]
 public class SomeTypeInspector: Editor
 {
   ...
 }



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
1

Answer by CaseyHofland · Jul 24, 2020 at 10:36 AM

Then why not just reselect the objects?

 public override void OnInspectorGUI()
 {
     EditorGUI.BeginChangeCheck();
     // Some code
 
     if(EditorGUI.EndChangeCheck())
     {
         // More code
 
         Selection.objects = new[] { ((MyType)target).gameObject };
         EditorApplication.delayCall += () => Selection.objects = Array.ConvertAll(targets, target => ((MyType)target).gameObject);
     }
 }

Instead of trying all kinds of repaint trickery that doesn't work because the objects aren't even selected, just reselect them.

Old but will post it for someone else looking for this.

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
1

Answer by abdel · Dec 12, 2020 at 02:14 PM

You can just call the function Repaint() inside your Editor code and it will refresh your Inspector.

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.

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

51 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 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

EditorGUI add SortingLayer-like list to custom Editor 1 Answer

Insert new custom class element with _default_ values to a SerializedProperty array? 1 Answer

Custom Editor problems 2 Answers

Create inspector drop-down button based on the content of a list in editor mode 1 Answer

Is it possible to drag/drop/increment into editor array? 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