Skip to content

Buttons

Simon edited this page Apr 22, 2021 · 9 revisions

Namespace: WIDVE.Utilities
Location: WIDVE Unity Scripts/Utilities/Buttons

A set of scripts that lets you store buttons and input methods as ScriptableObjects. Many of the other scripts in this package use buttons in this way.

Since buttons are ScriptableObjects, their settings can be edited by clicking on each button in the Project tab. These settings will be shared by any script using that same button object. If you need to use the same input with different settings for different scripts, simply create an additional Button object with the same input.

Creating Buttons

Individual buttons can be created using the Create/Button/... menu.
A set of the most commonly used buttons can be generated automatically by using the top menu item WIDVE/Generate Mouse and Keyboard Buttons. Buttons can be generated for other platforms as well.

When using a script that needs a Button object, clicking on the small dot next to the button field will bring up a popup menu showing all buttons available in the project. Choose one from here, or drag one in from the Project tab.

Examples

The MKBButton class contains a good example of how to create buttons. For mouse and keyboard input, these buttons use Unity's built in input system. Currently, buttons exist for Oculus devices as well.

Components

ButtonEvent

This is a simple component that invokes a UnityEvent in response to a button press. It requires a ButtonFloat that implements the GetDown method.

Button Types

Button

All buttons inherit from the Button<T> base class. The generic parameter determines the type of value the button outputs, and must be a value type (bool, int, float, Vector2, etc).
Note: button outputs are not scaled by Time.deltaTime. If deltaTime scaling is desired, this should be done in the script that uses the button.

Property Description
Smoothing The smoothing curve influences the final button output based on the raw input value. Both dimensions of the curve should remain in the range [0, 1]
Multiplier The button's output value will be multiplied by this amount. Can be used to quickly invert a button's output
Active When inactive, a button should always return a value of 0

GetSmoothedFloat

The GetSmoothedFloat method serves as a quick way to get a float value modified by the button's current Smoothing and Multiplier values. It does not check if the button is active or inactive.

Abstract Methods

These methods must be implemented in any custom button classes.

GetRawValue

The GetRawValue method uses the input system to get the button's current input value and returns that value in the range [0, 1].

GetValue

The GetValue method returns the button's current raw output value, adjusted by the current Smoothing, Multiplier, and Active state.

ButtonFloat

A ButtonFloat is simply a button that returns a float value. It includes a few additional methods that make it useful as a simple on/off button in many cases.
Note: Since some input devices have no simple way of implementing the Get methods, they are virtual, not abstract. If the derived button class does not implement them, they will always return false.

GetHeld

The GetHeld method returns true if the button is currently held down, false otherwise. It will return true on the same frame GetDown returns true, and false on the same frame GetUp returns true.

GetDown

The GetDown method returns true on the first frame a button is pressed, false otherwise.

GetUp

The GetUp method returns true on the first frame a button is released, false otherwise.

ButtonVector2

A ButtonVector2 is a button that returns output along a two-dimensional axis as a Vector2. This is useful for joystick input. Both input axes are evaluated using the same Smoothing and Multiplier values.

Clone this wiki locally