How to manually refresh Inspector from Editor code?
Asked Answered
D

4

0

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!

Desinence answered 18/10, 2023 at 8:57 Comment(2)

"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.

Fullblown

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

Fullblown
K
0

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
{
	//...
}
Kbp answered 16/1 at 13:36 Comment(0)
A
0

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.

Adjudge answered 9/10, 2023 at 9:27 Comment(0)
I
0

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

Iroquoian answered 5/12, 2023 at 15:18 Comment(0)
D
0

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

Dearly answered 18/10, 2023 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.