import { _decorator, Component, Node, Button, Label, ScrollView, resources, instantiate, Prefab } from 'cc'; const { ccclass, property } = _decorator; @ccclass('ResourceExplorer') export class ResourceExplorer extends Component { @property({ type: Label, tooltip: '资源路径标签' }) pathLabel: Label = null; @property({ type: Button, tooltip: '扫描按钮' }) scanButton: Button = null; @property({ type: Label, tooltip: '结果标签' }) resultLabel: Label = null; @property({ tooltip: '要扫描的资源路径' }) resourcePath: string = 'avatars'; start() { // 注册扫描按钮点击事件 if (this.scanButton) { this.scanButton.node.on(Button.EventType.CLICK, this.onScanButtonClick, this); } // 更新路径显示 this.updatePathDisplay(); } updatePathDisplay() { if (this.pathLabel) { this.pathLabel.string = `当前路径: ${this.resourcePath}`; } } onScanButtonClick() { this.scanResources(); } scanResources() { this.showResult(`正在扫描路径: ${this.resourcePath}...`); resources.loadDir(this.resourcePath, (err, assets) => { if (err) { this.showResult(`扫描失败: ${err.message}`); return; } let result = `找到 ${assets.length} 个资源:\n\n`; if (assets.length === 0) { result += "没有找到资源,请确认路径是否正确。"; } else { assets.forEach((asset, index) => { result += `${index + 1}. ${asset.name} (${asset.constructor ? asset.constructor.name : '未知类型'})\n`; }); } this.showResult(result); }); } showResult(message: string) { if (this.resultLabel) { this.resultLabel.string = message; } console.log(message); } setResourcePath(path: string) { this.resourcePath = path; this.updatePathDisplay(); } navigateToParent() { const parts = this.resourcePath.split('/'); if (parts.length > 1) { parts.pop(); this.resourcePath = parts.join('/'); } else { this.resourcePath = ''; } this.updatePathDisplay(); } navigateToChild(childName: string) { if (this.resourcePath) { this.resourcePath += '/' + childName; } else { this.resourcePath = childName; } this.updatePathDisplay(); } onDestroy() { // 清理事件监听 if (this.scanButton) { this.scanButton.node.off(Button.EventType.CLICK, this.onScanButtonClick, this); } } }