“即时模式”GUI 系统(也称为 IMGUI)是一个完全独立的功能系统,不同于 Unity 基于游戏对象的主 UI 系统。IMGUI 是一个代码驱动的 GUI 系统,主要用作程序员的工具。为了驱动该系统,需在实现脚本上调用 OnGUI 函数。例如,以下代码:
void OnGUI() {
if (GUILayout.Button("Press Me"))
Debug.Log("Hello!");
}
将显示如下所示的按钮:
即时模式 GUI 系统常用于:
The IMGUI system is not generally intended to be used for normal in-game user interfaces that players might use and interact with. For that you should use Unity’s main GameObject-based UI system, which offers a GameObject-based approach for editing and positioning UI elements, and has far better tools to work with the visual design and layout of the UI.
“立即模式”指的是创建和绘制 IMGUI 的方式。要创建 IMGUI 元素,必须编写进入名为 OnGUI 的特殊函数的代码。显示界面的代码将在每帧执行,并绘制到屏幕上。除了 OnGUI 代码附加到的对象,或者层级视图中与绘制的可视元素相关的其他类型对象之外,没有其他持久性游戏对象。
IMGUI 允许使用代码创建各种功能 GUI。通过该系统,无需创建游戏对象,手动定位这些对象,然后编写一个处理对象功能的脚本,而只需几行代码即可立即执行所有操作。该代码将生成通过单个函数调用进行绘制和处理的 GUI 控件。
本部分将介绍如何在游戏和 Unity Editor 的扩展中使用 IMGUI。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.