PersonalInfoManager.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { _decorator, Component, Node, Button, Sprite, Label, SpriteFrame, resources } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('PersonalInfoManager')
  4. export class PersonalInfoManager extends Component {
  5. @property({
  6. type: Node,
  7. tooltip: '个人资料UI面板'
  8. })
  9. personalInfoPanel: Node = null;
  10. @property({
  11. type: Button,
  12. tooltip: '关闭按钮'
  13. })
  14. closeButton: Button = null;
  15. @property({
  16. type: Sprite,
  17. tooltip: '角色头像显示'
  18. })
  19. characterAvatar: Sprite = null;
  20. @property({
  21. type: Label,
  22. tooltip: '角色信息文本'
  23. })
  24. infoText: Label = null;
  25. // 当前显示的角色ID
  26. private currentCharacterId: number = -1;
  27. start() {
  28. // 初始化隐藏面板
  29. if (this.personalInfoPanel) {
  30. this.personalInfoPanel.active = false;
  31. }
  32. // 注册关闭按钮事件
  33. this.setupCloseButton();
  34. }
  35. private setupCloseButton() {
  36. if (this.closeButton) {
  37. this.closeButton.node.off('click');
  38. this.closeButton.node.on('click', () => {
  39. this.hidePersonalInfoPanel();
  40. }, this);
  41. }
  42. }
  43. /**
  44. * 显示个人资料面板
  45. */
  46. public showPersonalInfoPanel() {
  47. if (this.personalInfoPanel) {
  48. this.personalInfoPanel.active = true;
  49. this.personalInfoPanel.setSiblingIndex(999);
  50. }
  51. }
  52. /**
  53. * 隐藏个人资料面板
  54. */
  55. public hidePersonalInfoPanel() {
  56. if (this.personalInfoPanel) {
  57. this.personalInfoPanel.active = false;
  58. }
  59. }
  60. /**
  61. * 显示角色资料
  62. * @param data 角色资料数据
  63. */
  64. public displayCharacterInfo(data: any) {
  65. if (!data) {
  66. console.error('角色资料数据为空');
  67. return;
  68. }
  69. // 保存当前显示的角色ID
  70. this.currentCharacterId = data.characterId;
  71. // 设置文本信息
  72. if (this.infoText && data.info) {
  73. this.infoText.string = data.info;
  74. }
  75. // 加载并设置头像
  76. if (this.characterAvatar && data.avatarPath) {
  77. resources.load(data.avatarPath, SpriteFrame, (err, spriteFrame) => {
  78. if (err) {
  79. console.error(`加载头像失败: ${data.avatarPath}`, err);
  80. return;
  81. }
  82. this.characterAvatar.spriteFrame = spriteFrame;
  83. });
  84. }
  85. // 显示面板
  86. this.showPersonalInfoPanel();
  87. }
  88. onDestroy() {
  89. if (this.closeButton) {
  90. this.closeButton.node.off('click');
  91. }
  92. }
  93. }