Unity GUI Button - Polling Input VS Event Driven Input

大城市里の小女人 提交于 2019-12-02 11:29:05
Programmer

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.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?

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.


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.

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

public Button button;

Register Button Event

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

Un-Register Button Event

button.onClick.RemoveAllListeners();

You can find other UI event examples here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!