Unity GUI按钮-轮询输入VS事件驱动输入 [英] Unity GUI Button - Polling Input VS Event Driven Input

查看:277
本文介绍了Unity GUI按钮-轮询输入VS事件驱动输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#学习Unity并研究GUI按钮.我在Unity文档中找到以下语句:

I am learning Unity with C# and studying the GUI Button. I found the following statement in Unity documentation:

这意味着您的OnGUI实现可能每帧被调用几次(每个事件一次)."

"This means that your OnGUI implementation might be called several times per frame (one call per event). "

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnGUI() {
        if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
            print("You clicked the button!");

    }
}

我的问题是:

1)上面的"if"语句将继续检测该条件,直到它为真为止.它应该称为轮询输入.为什么上面的代码为什么使用轮询输入而不是事件驱动的输入? (即,当有人按下按钮时,事件将被触发.).尽管如果条件为false,则if语句将不执行任何操作,但与事件驱动的输入相比,if语句一直处于检测状态,并且效率不高.

1) The above "if" statement will keep detecting the condition until it is true. It should be called polling input. Why does the above code use polling input instead of event driven input? (i.e. When someone press the button, the event is fired.). Although the if-statement will do nothing if the condition is false, it is keep detecting and not efficient compared with event driven input.

2)在这种情况下,为什么Unity使用轮询输入?

2) Why Unity use polling input in this case?

3)上面的陈述提到每个事件一个呼叫".这是否意味着它实际上是事件驱动的输入,而不是轮询输入?

3) The above statement mention that "one call per event". Does it mean it is actually a event driven input, not polling input?

我对这些问题感到困惑,无法找到答案.有人可以向我解释.谢谢.

I am confusing about these questions and can't find the answer. Would someone explain to me. Thanks.

推荐答案

1 .如果按下按钮,则GUI.Button返回true.如果未按下按钮,GUI.Button将返回false.就这么简单.那不是大事.它只是一个根据是否按下按钮而返回true或false的函数.

1.If you press a Button GUI.Button returns true. If the Button is not pressed, GUI.Button will return false. It's as simple as that. That's not an event. It is simply a function that returns true or false depending on if the Button is pressed or not.

2 .我的猜测是Unity希望通过这样做使事情变得更容易.基本上,您将创建一个Button并检查是否仅用一行代码就将其按下.

2.My guess is that Unity wanted to make things easier by doing this. Basically, you creating a Button and checking if it is pressed with just one line of code.

3

这是否意味着它实际上是事件驱动的输入,而不是轮询输入?

Does it mean it is actually a event driven input, not polling input?

它仍处于轮询状态,但单击直到释放才被调用一次.若要调用GUI.Button,必须在OnGUI函数中连续调用GUI.Button.

It is still Polling but it is called only once when clicked until released. For GUI.Button to be called, you must continuously call GUI.Button in the OnGUI function.

此时忘记GUI.Button.您现在使用的是 IMGUI . 不用了有一个新的UI系统(uGUI).这就是您应该使用的内容,所有内容都基于事件. 此处是新UI系统的教程.忽略任何需要使用OnGUI函数的内容.

Forget about GUI.Button at this time. What you are using now is called IMGUI. You don't need it. There is a new UI system(uGUI). This is what you should be using and everything is based on event. Here is a tutorial for the new UI system. Ignore anything that requires the use of OnGUI function.

以下是注册和注销Button onClick事件的简单方法.

Below is a simple way to register and unregister to the Button onClick event.

public Button button;

注册按钮事件

button.onClick.AddListener(() => yourCallBackFunction);

取消注册按钮事件

button.onClick.RemoveAllListeners();

您可以在此处找到其他UI事件示例.

You can find other UI event examples here.

这篇关于Unity GUI按钮-轮询输入VS事件驱动输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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