import { _decorator, Component, Node, Sprite, Label, resources, SpriteFrame, Color, CCInteger } from 'cc'; const { ccclass, property } = _decorator; @ccclass('SimpleCharacterView') export class SimpleCharacterView extends Component { @property({ type: Sprite, tooltip: '头像1显示' }) avatarSprite1: Sprite = null; @property({ type: Sprite, tooltip: '头像2显示' }) avatarSprite2: Sprite = null; @property({ type: Sprite, tooltip: '头像3显示' }) avatarSprite3: Sprite = null; @property({ type: Sprite, tooltip: '头像4显示' }) avatarSprite4: Sprite = null; @property({ type: Label, tooltip: '状态标签' }) statusLabel: Label = null; @property({ tooltip: '角色1 ID', type: CCInteger }) characterId1: number = 1; @property({ tooltip: '角色2 ID', type: CCInteger }) characterId2: number = 3; @property({ tooltip: '角色3 ID', type: CCInteger }) characterId3: number = 5; @property({ tooltip: '角色4 ID', type: CCInteger }) characterId4: number = 7; @property({ tooltip: '头像变体索引', type: CCInteger }) avatarVariant: number = 5; // 加载成功的计数 private successCount: number = 0; private totalTests: number = 0; start() { // 加载并显示所有头像 this.loadAllAvatars(); } loadAllAvatars() { this.updateStatus('开始加载头像...'); this.successCount = 0; this.totalTests = 0; // 加载每个角色的头像 if (this.avatarSprite1 && this.characterId1 > 0) { this.loadAvatar(this.characterId1, this.avatarSprite1, 1); } if (this.avatarSprite2 && this.characterId2 > 0) { this.loadAvatar(this.characterId2, this.avatarSprite2, 2); } if (this.avatarSprite3 && this.characterId3 > 0) { this.loadAvatar(this.characterId3, this.avatarSprite3, 3); } if (this.avatarSprite4 && this.characterId4 > 0) { this.loadAvatar(this.characterId4, this.avatarSprite4, 4); } } loadAvatar(characterId: number, targetSprite: Sprite, slotNumber: number) { this.totalTests++; // 构建头像路径 - 使用新的下划线格式 const avatarPath = `avatars/${characterId}/avatar_${characterId}_${this.avatarVariant}/spriteFrame`; console.log(`[${slotNumber}] 尝试加载: ${avatarPath}`); // 加载头像 resources.load(avatarPath, SpriteFrame, (err, spriteFrame) => { if (err) { console.error(`[${slotNumber}] 加载失败: ${avatarPath}`, err); // 尝试备用路径 this.tryFallbackPaths(characterId, targetSprite, slotNumber); } else { targetSprite.spriteFrame = spriteFrame; this.successCount++; console.log(`[${slotNumber}] 加载成功: ${avatarPath}`); this.updateStatus(`成功加载 ${this.successCount}/${this.totalTests} 个头像`); } }); } tryFallbackPaths(characterId: number, targetSprite: Sprite, slotNumber: number) { // 备用路径列表 - 使用新的下划线格式 const fallbackPaths = [ `avatars/${characterId}/avatar_${characterId}_1`, `avatars/${characterId}/${characterId}`, `0${characterId}/${characterId}` ]; let tried = 0; // 尝试下一个路径 const tryNextPath = (index: number) => { if (index >= fallbackPaths.length) { console.error(`[${slotNumber}] 所有路径尝试失败`); return; } tried++; const path = fallbackPaths[index]; console.log(`[${slotNumber}] 尝试备用路径${tried}: ${path}`); resources.load(path, SpriteFrame, (err, spriteFrame) => { if (err) { // 尝试下一个 tryNextPath(index + 1); } else { targetSprite.spriteFrame = spriteFrame; this.successCount++; console.log(`[${slotNumber}] 备用路径加载成功: ${path}`); this.updateStatus(`成功加载 ${this.successCount}/${this.totalTests} 个头像`); } }); }; // 开始尝试备用路径 tryNextPath(0); } updateStatus(message: string) { if (this.statusLabel) { this.statusLabel.string = message; } console.log('[SimpleCharacterView]', message); } }