[笔记][Unity3D]关于EditorWindow的OnGUI方法在非激活情况下不执行

4,185 阅读1分钟

如图:

我创建了一个EditorWindow,在OnGUI方法中进行绘制界面。

private static void ShowWindow() 
{
    var window = GetWindow<I18nKeyComponentShowWindow>(false, "展示选中的UI对象");
    window.autoRepaintOnSceneChange = true;
    window.Show();
}

void OnGUI() 
{
    GUILayout.Label("按住Ctrl,在激活Unity Game窗口的情况下,鼠标移过UI");
    UpdateList();
}

OnGUI在此窗口激活(被选中)的情况下,是会一直被调用的。然而在非激活情况下,就不生效了,这个时候需要加一个处理:

void OnInspectorUpdate()
{
    //开启窗口的重绘,不然窗口信息不会刷新
    Repaint();
}

附上OnInspectorUpdate方法的官方api: docs.unity3d.com/ScriptRefer…

OnInspectorUpdate is called at 10 frames per second to give the inspector a chance to update.