import { _decorator, Component, Node } from 'cc'; import { PhoneManager } from './PhoneManager'; const { ccclass, property } = _decorator; /** * 电话触发器,用于点击场景中的电话物体时激活电话UI和对话 */ @ccclass('PhoneTrigger') export class PhoneTrigger extends Component { @property({ type: PhoneManager, tooltip: '电话管理器引用' }) phoneManager: PhoneManager = null; @property({ tooltip: '是否启用触发器' }) isEnabled: boolean = true; start() { // 注册节点点击事件 this.node.on(Node.EventType.TOUCH_END, this.onPhoneClicked, this); } /** * 电话被点击时的处理 */ private onPhoneClicked() { if (!this.isEnabled) { return; } console.log('电话被点击'); // 如果有电话管理器引用,则显示电话UI和对话 if (this.phoneManager) { this.phoneManager.showPhonePanel(); } else { console.error('未设置电话管理器引用,无法显示电话UI'); } } /** * 启用触发器 */ public enable() { this.isEnabled = true; } /** * 禁用触发器 */ public disable() { this.isEnabled = false; } onDestroy() { // 移除事件监听 this.node.off(Node.EventType.TOUCH_END, this.onPhoneClicked, this); } }