| 12345678910111213141516171819202122232425262728293031323334 |
- // !单例模式
- export default class CommonUtility {
- private static instance: CommonUtility;
- private constructor() {}
-
- static getInstance() {
- if (!this.instance) {
- this.instance = new CommonUtility();
- }
- return this.instance;
- }
- bindNodeHandler(curComp: any, bNode: cc.Node, callback: string, isButton: boolean = true, customData: string = null, isRebind: boolean = false) {
- if (!curComp || !bNode) {
- return null;
- }
- let nodeComp = bNode.getComponent(isButton ? cc.Button : cc.Toggle);
- if (!nodeComp) {
- nodeComp = bNode.addComponent(isButton ? cc.Button : cc.Toggle);
- }
- var pHandler = new cc.Component.EventHandler();
- pHandler.target = curComp.node;
- pHandler.component = cc.js.getClassName(curComp);
- pHandler.handler = callback;
- if (customData != null) {
- pHandler.customEventData = customData;
- }
- if (isRebind) {
- nodeComp.clickEvents = [];
- }
- nodeComp.clickEvents.push(pHandler);
- return nodeComp;
- }
- }
|