I have a custom editor inspector that I have labels that report data that gets sync 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 values changes?
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 #ifUNITY_EDITOR, so it gets compiled away in a build.)
Posting this here since this is the closest match to my search: “unity refresh inspector”.
In my particular case I was trying to refresh the inspector view from PreferencesGUI. I tried inheriting Editor, but to access Repaint it needs to be called from a static class, which is what PreferencesGUI needs to be declared as. Anyway, the simplest solution for me was to use this:
public static PreferencesGUI ()
{
...preferences source code here
// tell the inspector to redraw
if(Selection.activeTransform != null)
EditorUtility.SetDirty(Selection.activeTransform);
}
This thread is very old, but here’s an another alternative to Repaint() and SetDirty(yourObject) for anyone with a similar problem to me: EditorUtility.SetDirty(**yourWindow**)