PhoneTrigger.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator, Component, Node } from 'cc';
  2. import { PhoneManager } from './PhoneManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 电话触发器,用于点击场景中的电话物体时激活电话UI和对话
  6. */
  7. @ccclass('PhoneTrigger')
  8. export class PhoneTrigger extends Component {
  9. @property({
  10. type: PhoneManager,
  11. tooltip: '电话管理器引用'
  12. })
  13. phoneManager: PhoneManager = null;
  14. @property({
  15. tooltip: '是否启用触发器'
  16. })
  17. isEnabled: boolean = true;
  18. start() {
  19. // 注册节点点击事件
  20. this.node.on(Node.EventType.TOUCH_END, this.onPhoneClicked, this);
  21. }
  22. /**
  23. * 电话被点击时的处理
  24. */
  25. private onPhoneClicked() {
  26. if (!this.isEnabled) {
  27. return;
  28. }
  29. console.log('电话被点击');
  30. // 如果有电话管理器引用,则显示电话UI和对话
  31. if (this.phoneManager) {
  32. this.phoneManager.showPhonePanel();
  33. } else {
  34. console.error('未设置电话管理器引用,无法显示电话UI');
  35. }
  36. }
  37. /**
  38. * 启用触发器
  39. */
  40. public enable() {
  41. this.isEnabled = true;
  42. }
  43. /**
  44. * 禁用触发器
  45. */
  46. public disable() {
  47. this.isEnabled = false;
  48. }
  49. onDestroy() {
  50. // 移除事件监听
  51. this.node.off(Node.EventType.TOUCH_END, this.onPhoneClicked, this);
  52. }
  53. }