Button Component Reference

The Button component responds to a click from the user. When the user clicks a Button, its status will change. In addition, users can assign a custom behavior to buttons’ click event.

button.png

button-color

Click the Add Component button at the bottom of the Inspector panel and select UI/Button to add the Button component to the node.

To use Button, please refer to the Button API documentation and the Button scene of the test-cases-3d project.

Button Properties

PropertyFunction Explanation
TargetSpecify the Button background node. When the Button status changes, the Color or Sprite property of the node will be modified.
InteractableBoolean type, if set to false then the Button component enters the forbidden state.
TransitionEnumeration type, including NONE, COLOR, SPRITE and SCALE. Each type corresponds to a different Transition setting. Please see the Button Transition section below for details.
ClickEventsList type, default is null. Each event added by the user is composed of the node reference, component name and a response function. Please see the Button Event section below for details.

Button Transition

Button Transition is used to choose the action of the button when clicked by the user. Currently the types available are NONE, COLOR, SPRITE and SCALE.

transition

Color Transition

color-transition

PropertyFunction Explanation
NormalColor of Button under Normal status.
PressedColor of Button under Pressed status.
HoverColor of Button under Hover status.
DisabledColor of Button under Disabled status.
DurationTime interval needed for Button status switching.

Sprite Transition

sprite-transition

PropertyFunction Explanation
NormalSpriteFrame of Button under Normal status.
PressedSpriteFrame of Button under Pressed status.
HoverSpriteFrame of Button under Hover status.
DisabledSpriteFrame of Button under Disabled status.

Scale Transition

scaleTransition

PropertyFunction Explanation
DurationTime interval needed for Button status switching.
ZoomScaleWhen the user clicks the button, the button will zoom to a scale. The final scale of the button equals to the button’s original scale * zoomScale, and the zoomScale can be a negative value.

Button Click Events

The Button can additionally add a click event to respond to the player’s click action. There are two ways to achieve this.

Add a callback using the Properties

button-event

PropertyFunction Explanation
TargetNode with the script component.
ComponentScript component name.
HandlerAssign a callback function from the given component which will be triggered when the user clicks the Button.
CustomEventDataA user-defined string value passed as the last event argument of the event callback.

Add a callback using the script

There are two ways to add a callback through the script.

  1. The event callback added by this method is the same as the event callback added by the editor, all added by the script. First you need to construct a EventHandler object, and then set the corresponding target, component, handler and customEventData parameters.

    1. import { _decorator, Component, Event, Node, Button, EventHandler } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass("example")
    4. export class example extends Component {
    5. onLoad () {
    6. const clickEventHandler = new EventHandler();
    7. // This node is the node to which your event handler code component belongs
    8. clickEventHandler.target = this.node;
    9. // This is the script class name
    10. clickEventHandler.component = 'example';
    11. clickEventHandler.handler = 'callback';
    12. clickEventHandler.customEventData = 'foobar';
    13. const button = this.node.getComponent(Button);
    14. button.clickEvents.push(clickEventHandler);
    15. }
    16. callback (event: Event, customEventData: string) {
    17. // The event here is a Touch object, and you can get the send node of the event by event.target
    18. const node = event.target as Node;
    19. const button = node.getComponent(Button);
    20. console.log(customEventData); // foobar
    21. }
    22. }
  2. By button.node.on ('click', ...) to add event callback. This is a very simple way, but the way has some limitations, in the event callback the screen coordinate point of the current click button cannot be obtained.

    1. // Suppose we add an event handler callback to the onLoad method of a component and handle the event in the callback function:
    2. import { _decorator, Component, Button } from 'cc';
    3. const { ccclass, property } = _decorator;
    4. @ccclass("example")
    5. export class example ex tends Component {
    6. @property(Button)
    7. button: Button | null = null;
    8. onLoad () {
    9. this.button.node.on(Button.EventType.CLICK, this.callback, this);
    10. }
    11. callback (button: Button) {
    12. // Note that events registered this way cannot pass customEventData
    13. }
    14. }