Module components_ui_button
Interactive UI button backed by a BoxCollider.
Info:
- Author: Sharper Dev
Functions
| Button:new (gameObject) | Creates a button and binds its input handlers to the object's BoxCollider. |
| Button:update () | Per-frame update; keeps the assigned image positioned with the button. |
| Button:setCanvas (canvas) | Adds the button to a canvas, removing it from its previous canvas if present. |
| Button:setSize (width, height) | Sets the size of the button. |
| Button:setImageComponent (imageComponent) | Sets the image component for the button. |
| Button:setTextComponent (textComponent) | Sets the text component for the button. |
| Button:destroy () | Destroys the button, removing it from the canvas and cleaning up resources. |
Events
| Button.onDown () | Override for the press event, triggered by the collider's touch-down callback. |
| Button.onHold () | Override for the held event; this method is not invoked by this component's update logic. |
| Button.onUp () | Override for the release event, triggered by the collider's touch-up callback. |
| Button.onClick () | Override for the click event, triggered by the collider's touch-click callback. |
Functions
- Button:new (gameObject)
-
Creates a button and binds its input handlers to the object's BoxCollider.
Parameters:
- gameObject table Game object that owns the button and its BoxCollider.
Returns:
-
table The new Button instance.
Usage:
local button = gameObject:addComponent("Button")
- Button:update ()
- Per-frame update; keeps the assigned image positioned with the button.
- Button:setCanvas (canvas)
-
Adds the button to a canvas, removing it from its previous canvas if present.
Parameters:
- canvas table Canvas that will contain the button.
Returns:
-
table This button.
Usage:
button:setCanvas(canvas) - Button:setSize (width, height)
-
Sets the size of the button.
It is necessary to calculate the collision box size.
Parameters:
- width The width of the button.
- height The height of the button.
Returns:
-
The button instance.
Usage:
button:setSize(100, 50)
- Button:setImageComponent (imageComponent)
-
Sets the image component for the button.
When the button contains an image component, its size will be set to match the image dimensions.
Parameters:
- imageComponent The image component to set.
Returns:
-
The button instance.
Usage:
button:setImageComponent(imageComponent) - Button:setTextComponent (textComponent)
-
Sets the text component for the button.
Parameters:
- textComponent The text component to set.
Returns:
-
The button instance.
Usage:
button:setTextComponent(textComponent) - Button:destroy ()
-
Destroys the button, removing it from the canvas and cleaning up resources.
It is called automatically when the scene changes.
Events
- Button.onDown ()
-
Override for the press event, triggered by the collider's touch-down callback.
Usage:
function button.onDown() -- Code here end
- Button.onHold ()
- Override for the held event; this method is not invoked by this component's update logic.
- Button.onUp ()
-
Override for the release event, triggered by the collider's touch-up callback.
Usage:
function button.onUp() -- Code here end
- Button.onClick ()
-
Override for the click event, triggered by the collider's touch-click callback.
Usage:
function button.onClick() -- Code here end