Unity - 自定义编辑器 - 数据刷新 [英] Unity - Custom Editor - data refresh

查看:224
本文介绍了Unity - 自定义编辑器 - 数据刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您可以使用 Update<块引用>

每秒在所有可见窗口上调用多次.

喜欢

private void Update(){重绘();}


然而这可能会变得相当昂贵,我认为你也可以使用 OnInspectorUpdate

<块引用>

OnInspectorUpdate 以每秒 10 帧的速度调用,让检查器有机会更新.

就像在示例中:

void OnInspectorUpdate(){//在重新绘制窗口时在 OnInspectorUpdate 上调用 Repaint//比 OnGUI/Update 更少的次数重绘();}

所以它只能以 10 帧/秒的速度运行,但如果您不这样做,它也会连续运行,例如移动鼠标.

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

I created a new window in the editor. It is intended for debugging and creating some mini test scenarios. For example, reducing or increasing the HP of enemies, subtracting resources, etc. In the OnGUI method - I create a loop that iterates through the list of opponents and collects information about HP, ammunition etc. Via GUILayout.Label I display this information. Unfortunately, this data does not refresh dynamically. And every few seconds or clicking on the window. I don't know if I'm using this editor well. But I would like to have a section where this data will dynamically refresh for me - and will be part of the UI of the UnityEditor, not the Game itself.

解决方案

You could use Update

Called multiple times per second on all visible windows.

like

private void Update()
{
    Repaint();
}


This is can however get quite expensive and I think you could also rather use OnInspectorUpdate

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

like in the example:

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

so it only runs at 10 frames/second but continuously so also if you don't e.g. move the mouse.

Repaint basically forces a new OnGUI run.

这篇关于Unity - 自定义编辑器 - 数据刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆