Как проверить нажата ли кнопка unity

от admin

Input.GetKeyDown

Возвращает true, если на протяжении кадра пользователь начинает выполнять нажатие клавиши name .

You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again.

For the list of key identifiers see Conventional Game Input. When dealing with input it is recommended to use Input.GetAxis and Input.GetButton instead since it allows end-users to configure the keys.

Description

Возвращает true, если на протяжении кадра пользователь начинает выполнять нажатие клавиши, идентифицированной параметром key перечисления KeyCode.

How to detect if button is clicked unity?

I am not sure how to test if a button is clicked in unity 19.4. This is the code I tested does anyone know how to test things in the Update Method?

1 Answer 1

Buttons work by adding listeners to the event of their onclick . These listeners can be persistent or runtime listeners. Persistent listeners are assigned in the editor and the runtime listeners are assigned in code at runtime. Here is how you would assign a new listener in code.

In the above example, I am adding two listeners. I am not sure if the function you want to call will have parameters or not, so make sure to only add a single callback unless you want multiple functions called when your button is clicked.

Update is a method that is called every frame. Only put logic in here that is continually checked or updated often. A button should only react once it is clicked, which is why it has the onclick . Unity is already internally checking if a button is clicked by using its EventSystem and a series of GraphicalRaycasts .

I had asked which version of Unity you are in as in newer versions there is a newer UI flow called UI Builder which has a completely different method of adding onclick events.

Understanding GetButton and GetKey inputs in Unity

Unity’s button / Input system has been the source of confusion for many newcomers. It’s no suprise, after all we have GetKey, GetKeyDown, GetKeyUp, GetButton, GetButtonDown, GetButtonUp.

So let’s try make sense of it all.

GetKey / GetKeyDown / GetKeyUp

Input.GetKey(…) will be familiar to developers coming from other platforms like Flash. It accepts either a KeyCode parameter OR a literal string representing that key.
If you hold down the key it will continue to fire, so it’s the equivalent of ‘this key is being held down’. So here comes the confuser… GetKeyDown in fact represents a single key stroke.
If you hold down a key with GetKeyDown it will only fire once, and to reset it the key must me released.

Читать:
Dumpchk exe что это

GetKey = machine gun

finally we have the key released handler

GetButton / GetButtonDown/ GetButtonUp

The ‘get button’ functions work similarly to the ‘get key’ functions,
except you cannot use KeyCodes, rather you are required to set your
own buttons in the input manager. This is the recommended by
Unity and can be quite powerful as it allows developers to map custom
joystick / D-pad buttons.

By default Unity has already created some custom button inputs that can be accessed out of the box.

To access the input manager go to Edit > Settings > Input.

The Input manager looks like this

The Input Manager

I wont go into specific details of how the Input manager works in this particular post as it is pretty self explanatory, I may however cover it in more detail in a separate post.

How To Detect Any Key Press But Ignore Mouse – Unity C#

There might be a time you want to check if the player has pressed any key on the keyboard – but you do not want Unity to check for mouse clicks. This can be frustrating, because for some reason, the line of code you would expect to work checks for both keyboard presses AND mouse clicks:

I don’t know why Unity didn’t separate out mouse clicks and keyboard presses into two separate lines of code, but there it is. This is something I frequently had to fight Unity about while developing Puzzledorf, particularly when designing the menu’s and the title screen.

How To Do It

Fortunately, the workaround is simple:

The solution is a slightly more complicated if statement. First we check if any key is down, including mouse clicks, but then we also make sure that none of the mouse buttons are being pressed.

If you are confused by what &&, || and ! mean in the if statement, read my article here explaining them.

Join the Discord. Talk about Game Dev. Talk about Gaming.

Похожие статьи