Unity - 自定义编辑器 - 数据刷新

Unity - Custom Editor - data refresh

我在编辑器中创建了一个新的 window。它用于调试和创建一些小型测试场景。例如减少或增加敌人的生命值,减少资源等。 在 OnGUI 方法中 - 我创建了一个循环遍历对手列表并收集有关 HP、弹药等的信息。通过 GUILayout.Label 我显示了这些信息。 不幸的是,此数据不会动态刷新。每隔几秒或点击 window。 我不知道我是否用好这个编辑器。但我想要一个部分,其中这些数据将为我动态刷新 - 并且将成为 UnityEditor UI 的一部分,而不是游戏本身。

您可以使用 Update

Called multiple times per second on all visible windows.

喜欢

private void Update()
{
    Repaint();
}

这可能会变得非常昂贵,我想你也可以使用 OnInspectorUpdate

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

如示例所示:

void OnInspectorUpdate()
{
    // Call Repaint on OnInspectorUpdate as it repaints the windows
    // less times as if it was OnGUI/Update
    Repaint();
}

所以它在 10 frames/second 时只有 运行s,但如果你不这样做,也会持续如此,例如移动鼠标。

Repaint 基本上强制一个新的 OnGUI 运行.

您可以使用OnInsepctorUpdate(),或者如果您想要与您的游戏运行速度相同的东西,您可以在Update()方法中调用Repaint()

// This will update your GUI on every frame.
private void Update() => Repaint();