123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import { _decorator, Component, Node, Button, Sprite, Label, SpriteFrame, resources } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('PersonalInfoManager')
- export class PersonalInfoManager extends Component {
- @property({
- type: Node,
- tooltip: '个人资料UI面板'
- })
- personalInfoPanel: Node = null;
-
- @property({
- type: Button,
- tooltip: '关闭按钮'
- })
- closeButton: Button = null;
-
- @property({
- type: Sprite,
- tooltip: '角色头像显示'
- })
- characterAvatar: Sprite = null;
-
- @property({
- type: Label,
- tooltip: '角色信息文本'
- })
- infoText: Label = null;
-
- // 当前显示的角色ID
- private currentCharacterId: number = -1;
-
- start() {
- // 初始化隐藏面板
- if (this.personalInfoPanel) {
- this.personalInfoPanel.active = false;
- }
-
- // 注册关闭按钮事件
- this.setupCloseButton();
- }
-
- private setupCloseButton() {
- if (this.closeButton) {
- this.closeButton.node.off('click');
- this.closeButton.node.on('click', () => {
- this.hidePersonalInfoPanel();
- }, this);
- }
- }
-
- /**
- * 显示个人资料面板
- */
- public showPersonalInfoPanel() {
- if (this.personalInfoPanel) {
- this.personalInfoPanel.active = true;
- this.personalInfoPanel.setSiblingIndex(999);
- }
- }
-
- /**
- * 隐藏个人资料面板
- */
- public hidePersonalInfoPanel() {
- if (this.personalInfoPanel) {
- this.personalInfoPanel.active = false;
- }
- }
-
- /**
- * 显示角色资料
- * @param data 角色资料数据
- */
- public displayCharacterInfo(data: any) {
- if (!data) {
- console.error('角色资料数据为空');
- return;
- }
-
- // 保存当前显示的角色ID
- this.currentCharacterId = data.characterId;
-
- // 设置文本信息
- if (this.infoText && data.info) {
- this.infoText.string = data.info;
- }
-
- // 加载并设置头像
- if (this.characterAvatar && data.avatarPath) {
- resources.load(data.avatarPath, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.error(`加载头像失败: ${data.avatarPath}`, err);
- return;
- }
-
- this.characterAvatar.spriteFrame = spriteFrame;
- });
- }
-
- // 显示面板
- this.showPersonalInfoPanel();
- }
-
- onDestroy() {
- if (this.closeButton) {
- this.closeButton.node.off('click');
- }
- }
- }
|