加载资源 cocos creator 3.0

本文介绍使用Cocos2d-x引擎加载不同类型的资源,包括单个资源、多个资源及整个文件夹的方法。提供了具体的代码示例,展示了如何通过回调函数监控加载进度并处理加载完成后的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假如说目前项目文件结构如下:

resources
|-images
  |-level-00000.jpg
  |-level-00001.jpg

加载单个资源:

cc.resources.load("images/level-00000", cc.SpriteFrame, function (err, spriteFrame) {
   console.log('single resources: ', spriteFrame)
})

加载多个资源

cc.resources.load([
    "images/level-00000", 
    "images/level-00001"], 
cc.SpriteFrame,
function(finished, total, item)  {
    console.log('arr progress', finished / total)
},
function (err, spriteFrames) {
    console.log('arr resources: ', err, spriteFrames) 
})

加载文件夹下资源:

cc.resources.loadDir('images', cc.SpriteFrame, function(finished, total, item)  {
    console.log('progress', finished / total)
}, function(err, spriteFrames) {
    console.log('resources', spriteFrames)
})

定义

export type CompleteCallbackWithData<T = any> = (err: Error | null, data: T) => void;
export type ProgressCallback = (finished: number, total: number, item: RequestItem) => void;
// load
public load<T extends Asset> (
    paths: string,
    type: AssetType<T> | null,
    onProgress: ProgressCallback | null,
    onComplete: CompleteCallbackWithData<T> | null): void;
public load<T extends Asset> (
    paths: string[], type: AssetType<T> | null,
    onProgress: ProgressCallback | null,
    onComplete: CompleteCallbackWithData<T[]> | null): void;
public load<T extends Asset> (paths: string, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<T> | null): void;
public load<T extends Asset> (paths: string[], onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<T[]> | null): void;
public load<T extends Asset> (paths: string, onComplete?: CompleteCallbackWithData<T> | null): void;
public load<T extends Asset> (paths: string[], onComplete?: CompleteCallbackWithData<T[]> | null): void;
public load<T extends Asset> (paths: string, type: AssetType<T> | null, onComplete?: CompleteCallbackWithData<T> | null): void;
public load<T extends Asset> (paths: string[], type: AssetType<T> | null, onComplete?: CompleteCallbackWithData<T[]> | null): void;
public load<T extends Asset> (
    paths: string|string[],
    type?: AssetType<T> | ProgressCallback | CompleteCallbackWithData<T|T[]> | null,
    onProgress?: ProgressCallback | CompleteCallbackWithData<T|T[]> | null,
    onComplete?: CompleteCallbackWithData<T|T[]> | null,
) {
    const { type: _type, onProgress: onProg, onComplete: onComp } = parseLoadResArgs(type, onProgress, onComplete);
    const options = { __requestType__: RequestType.PATH, type: _type, bundle: this.name, __outputAsArray__: Array.isArray(paths) };
    legacyCC.assetManager.loadAny(paths, options, onProg, onComp);
}}

// loadDir
public loadDir<T extends Asset> (dir: string, type: AssetType<T> | null, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<T[]> | null): void;
public loadDir<T extends Asset> (dir: string, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<T[]> | null): void;
public loadDir<T extends Asset> (dir: string, onComplete?: CompleteCallbackWithData<T[]> | null): void;
public loadDir<T extends Asset> (dir: string, type: AssetType<T> | null, onComplete?: CompleteCallbackWithData<T[]> | null): void;
public loadDir<T extends Asset> (
    dir: string,
    type?: AssetType<T> | ProgressCallback | CompleteCallbackWithData<T[]> | null,
    onProgress?: ProgressCallback | CompleteCallbackWithData<T[]> | null,
    onComplete?: CompleteCallbackWithData<T[]> | null,
) {
    const { type: _type, onProgress: onProg, onComplete: onComp } = parseLoadResArgs(type, onProgress, onComplete);
    legacyCC.assetManager.loadAny(dir, { __requestType__: RequestType.DIR, type: _type, bundle: this.name, __outputAsArray__: true }, onProg, onComp);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyccstudio

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值