Task Objectives
- This Skill is used to: master Cocos Creator runtime asset management
- Capabilities include: asset loading, asset caching, asset releasing, Asset Bundle
- Trigger conditions: dynamically loading assets, managing game assets, releasing memory
Overview
Asset Manager is the asset management module provided by Cocos Creator, used to help developers manage the use of assets, greatly improving development efficiency and user experience. Compared with the previous loader, it has better performance, an easier-to-use API, and stronger extensibility.
Core Features
Asset Loading
import { resources } from 'cc';
// Load a single asset resources.load('textures/gameBg', SpriteFrame, (err, spriteFrame) => { if (!err) { this.background.spriteFrame = spriteFrame; } });
// Batch loading
resources.load(['textures/btn', 'textures/icon'], (completedCount, totalCount) => {
const progress = completedCount / totalCount;
console.log(Loading: ${progress * 100}%);
}, (err, assets) => {
if (!err) {
console.log('All assets loaded');
}
});
### 资源查找
```typescript
import { resources } from 'cc';
// 查找资源(不加载)
const info = resources.getInfoById('textures/gameBg');
const info = resources.getInfoByUuid('xxx');
// 获取已加载的资源
const asset = resources.get('textures/gameBg');
Asset Releasing
import { resources } from 'cc';
// 释放单个资源
resources.release('textures/gameBg', SpriteFrame);
// 释放多个资源
resources.release(['textures/btn', 'textures/icon']);
// 释放指定资源及其依赖
resources.releaseUnusedAssets();
// 释放所有资源(慎用)
resources.releaseAll();
Asset Bundle
Concept
- Package assets into independent bundles
- Support hot updates
- Load on demand
Configuration
Create a bundle configuration under the assets directory:
{
"name": "levels",
"path": "resources/levels",
"compressionType": "zip"
}
Load Bundle
import { assetManager } from 'cc';
assetManager.loadBundle('levels', (err, bundle) => {
if (!err) {
bundle.load('level1', Prefab, (err, prefab) => {
if (!err) {
instantiate(prefab);
}
});
}
});
Bundle Management
// 获取已加载的 Bundle
const bundle = assetManager.getBundle('levels');
// 预加载 Bundle
assetManager.preloadBundle('levels', (progress, completedCount, totalCount) => {
console.log(`Preloading: ${completedCount}/${totalCount}`);
}, (err) => {
console.log('Preload complete');
});
// 卸载 Bundle
assetManager.removeBundle(bundle);
Remote Assets
Load Remote Assets
import { remote } from 'cc';
remote.loadImage('https://example.com/image.png', (err, spriteFrame) => {
if (!err) {
this.image.spriteFrame = spriteFrame;
}
});
Remote Bundle
remote.loadRemoteBundle('https://example.com/bundle', (err, bundle) => {
if (!err) {
bundle.load('prefab');
}
});
Asset Change Listening
Listen for Asset Changes
import { AssetManager } from 'cc';
AssetManager.AssetLibrary.loadAssetInfoInRuntime('uuid-of-asset', (err, assetInfo) => {
if (!err) {
console.log('Asset info:', assetInfo);
}
});
Extending the Asset Manager
Custom Downloader
import { assetManager } from 'cc';
assetManager.downloader.appendRegister({
exts: ['.myformat'],
downloader: (url, options, onComplete) => {
// 自定义下载逻辑
fetch(url)
.then(res => res.arrayBuffer())
.then(data => onComplete(null, data))
.catch(err => onComplete(err));
}
});
Asset Index
Required References
Notes
Memory Management
- Release unneeded assets promptly
- Use releaseUnusedAssets to clean up the cache
- Be careful of circular references
Path Conventions
- Dynamic loading paths are relative to the resources directory
- Do not include file extensions
- Use uuid or relative paths
Performance Optimization
- Make good use of preloading
- Batch loading reduces requests
- Use caching strategies appropriately