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!

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.GetType() == t)
		{
			ed.Repaint();
			return;
		}
	}
}

and to use

RepaintInspector( typeof(SomeTypeInspector) );

where

[CustomEditor(typeof(SomeType))]
public class SomeTypeInspector: Editor
{
	//...
}

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

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.

There is a crutch method if it doesn’t work for you like it does for me. Just turn off and turn on your script after hiding or displaying ^-^