SimpleCharacterView.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { _decorator, Component, Node, Sprite, Label, resources, SpriteFrame, Color, CCInteger } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('SimpleCharacterView')
  4. export class SimpleCharacterView extends Component {
  5. @property({
  6. type: Sprite,
  7. tooltip: '头像1显示'
  8. })
  9. avatarSprite1: Sprite = null;
  10. @property({
  11. type: Sprite,
  12. tooltip: '头像2显示'
  13. })
  14. avatarSprite2: Sprite = null;
  15. @property({
  16. type: Sprite,
  17. tooltip: '头像3显示'
  18. })
  19. avatarSprite3: Sprite = null;
  20. @property({
  21. type: Sprite,
  22. tooltip: '头像4显示'
  23. })
  24. avatarSprite4: Sprite = null;
  25. @property({
  26. type: Label,
  27. tooltip: '状态标签'
  28. })
  29. statusLabel: Label = null;
  30. @property({
  31. tooltip: '角色1 ID',
  32. type: CCInteger
  33. })
  34. characterId1: number = 1;
  35. @property({
  36. tooltip: '角色2 ID',
  37. type: CCInteger
  38. })
  39. characterId2: number = 3;
  40. @property({
  41. tooltip: '角色3 ID',
  42. type: CCInteger
  43. })
  44. characterId3: number = 5;
  45. @property({
  46. tooltip: '角色4 ID',
  47. type: CCInteger
  48. })
  49. characterId4: number = 7;
  50. @property({
  51. tooltip: '头像变体索引',
  52. type: CCInteger
  53. })
  54. avatarVariant: number = 5;
  55. // 加载成功的计数
  56. private successCount: number = 0;
  57. private totalTests: number = 0;
  58. start() {
  59. // 加载并显示所有头像
  60. this.loadAllAvatars();
  61. }
  62. loadAllAvatars() {
  63. this.updateStatus('开始加载头像...');
  64. this.successCount = 0;
  65. this.totalTests = 0;
  66. // 加载每个角色的头像
  67. if (this.avatarSprite1 && this.characterId1 > 0) {
  68. this.loadAvatar(this.characterId1, this.avatarSprite1, 1);
  69. }
  70. if (this.avatarSprite2 && this.characterId2 > 0) {
  71. this.loadAvatar(this.characterId2, this.avatarSprite2, 2);
  72. }
  73. if (this.avatarSprite3 && this.characterId3 > 0) {
  74. this.loadAvatar(this.characterId3, this.avatarSprite3, 3);
  75. }
  76. if (this.avatarSprite4 && this.characterId4 > 0) {
  77. this.loadAvatar(this.characterId4, this.avatarSprite4, 4);
  78. }
  79. }
  80. loadAvatar(characterId: number, targetSprite: Sprite, slotNumber: number) {
  81. this.totalTests++;
  82. // 构建头像路径 - 使用新的下划线格式
  83. const avatarPath = `avatars/${characterId}/avatar_${characterId}_${this.avatarVariant}/spriteFrame`;
  84. console.log(`[${slotNumber}] 尝试加载: ${avatarPath}`);
  85. // 加载头像
  86. resources.load(avatarPath, SpriteFrame, (err, spriteFrame) => {
  87. if (err) {
  88. console.error(`[${slotNumber}] 加载失败: ${avatarPath}`, err);
  89. // 尝试备用路径
  90. this.tryFallbackPaths(characterId, targetSprite, slotNumber);
  91. } else {
  92. targetSprite.spriteFrame = spriteFrame;
  93. this.successCount++;
  94. console.log(`[${slotNumber}] 加载成功: ${avatarPath}`);
  95. this.updateStatus(`成功加载 ${this.successCount}/${this.totalTests} 个头像`);
  96. }
  97. });
  98. }
  99. tryFallbackPaths(characterId: number, targetSprite: Sprite, slotNumber: number) {
  100. // 备用路径列表 - 使用新的下划线格式
  101. const fallbackPaths = [
  102. `avatars/${characterId}/avatar_${characterId}_1`,
  103. `avatars/${characterId}/${characterId}`,
  104. `0${characterId}/${characterId}`
  105. ];
  106. let tried = 0;
  107. // 尝试下一个路径
  108. const tryNextPath = (index: number) => {
  109. if (index >= fallbackPaths.length) {
  110. console.error(`[${slotNumber}] 所有路径尝试失败`);
  111. return;
  112. }
  113. tried++;
  114. const path = fallbackPaths[index];
  115. console.log(`[${slotNumber}] 尝试备用路径${tried}: ${path}`);
  116. resources.load(path, SpriteFrame, (err, spriteFrame) => {
  117. if (err) {
  118. // 尝试下一个
  119. tryNextPath(index + 1);
  120. } else {
  121. targetSprite.spriteFrame = spriteFrame;
  122. this.successCount++;
  123. console.log(`[${slotNumber}] 备用路径加载成功: ${path}`);
  124. this.updateStatus(`成功加载 ${this.successCount}/${this.totalTests} 个头像`);
  125. }
  126. });
  127. };
  128. // 开始尝试备用路径
  129. tryNextPath(0);
  130. }
  131. updateStatus(message: string) {
  132. if (this.statusLabel) {
  133. this.statusLabel.string = message;
  134. }
  135. console.log('[SimpleCharacterView]', message);
  136. }
  137. }