Toggle 组件参考
点击 属性检查器 下面的 添加组件 按钮,然后选择 UI/Toggle 即可添加 Toggle 组件到节点上。
注意:因为 Toggle 继承自 Button,所以关于 Toggle 的 Button 相关属性的详细说明和用法请参考 Button 组件。
Toggle 的事件回调有二个参数,第一个参数是 Toggle 本身, 第二个参数是 customEventData。
Toggle 组件的节点树一般为:
这种方法添加的事件回调和使用编辑器添加的事件回调是一样的,都是通过代码添加。首先需要构造一个 对象,然后设置好对应的 target
、component
、handler
和 customEventData
参数。
方法二
通过 toggle.node.on('toggle', ...)
的方式来添加
// 假设我们在一个组件的 onLoad 方法里面添加事件处理回调,在 callback 函数中进行事件处理:
import { _decorator, Component, ToggleComponent } from "cc";
const { ccclass, property } = _decorator;
@ccclass("example")
export class example extends Component {
@property(ToggleComponent)
this.toggle.node.on('toggle', this.callback, this);
}
callback(toggle: ToggleComponnet){
// 回调的参数是 toggle 组件,注意这种方式注册的事件,无法传递 customEventData
}