CommonUtility.ts 1021 B

12345678910111213141516171819202122232425262728293031323334
  1. // !单例模式
  2. export default class CommonUtility {
  3. private static instance: CommonUtility;
  4. private constructor() {}
  5. static getInstance() {
  6. if (!this.instance) {
  7. this.instance = new CommonUtility();
  8. }
  9. return this.instance;
  10. }
  11. bindNodeHandler(curComp: any, bNode: cc.Node, callback: string, isButton: boolean = true, customData: string = null, isRebind: boolean = false) {
  12. if (!curComp || !bNode) {
  13. return null;
  14. }
  15. let nodeComp = bNode.getComponent(isButton ? cc.Button : cc.Toggle);
  16. if (!nodeComp) {
  17. nodeComp = bNode.addComponent(isButton ? cc.Button : cc.Toggle);
  18. }
  19. var pHandler = new cc.Component.EventHandler();
  20. pHandler.target = curComp.node;
  21. pHandler.component = cc.js.getClassName(curComp);
  22. pHandler.handler = callback;
  23. if (customData != null) {
  24. pHandler.customEventData = customData;
  25. }
  26. if (isRebind) {
  27. nodeComp.clickEvents = [];
  28. }
  29. nodeComp.clickEvents.push(pHandler);
  30. return nodeComp;
  31. }
  32. }