Options
All
  • Public
  • Public/Protected
  • All
Menu

Class AssetManager

此模块管理资源的行为和信息,包括加载,释放等,这是一个单例,所有成员能够通过 cc.assetManager 调用

Hierarchy

  • AssetManager

Index

Properties

Private _files

_files: Cache<any> = files

Private _parsePipeline

_parsePipeline: null | Pipeline = BUILD ? null : new Pipeline('parse existing json', [this.loadPipe])

Private _parsed

_parsed: Cache<any> = parsed

Private _releaseManager

_releaseManager: ReleaseManager = releaseManager

allowImageBitmap

allowImageBitmap: boolean = !sys.isMobile

是否优先使用 image bitmap 来加载图片,启用之后,图片加载速度会更快, 但内存占用会变高,

assets

assets: Cache<Asset> = assets

已加载资源的集合, 你能通过 {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}} 来移除缓存

bundles

bundles: Cache<Bundle> = bundles

已加载 bundle 的集合, 你能通过 {{#crossLink "AssetManager/removeBundle:method"}}{{/crossLink}} 来移除缓存

cacheAsset

cacheAsset: boolean = true

是否缓存已加载的资源

cacheManager

cacheManager: CacheManager | null = null

缓存管理器是一个模块,在非 WEB 平台上,用于管理所有从服务器上下载下来的缓存

dependUtil

dependUtil: DependUtil = dependUtil

管理资源依赖关系

downloader

downloader: Downloader = downloader

管理所有下载任务

factory

factory: Factory = factory

fetchPipe

fetchPipe: IPipe = fetch

fetchPipeline

fetchPipeline: Pipeline = fetchPipeline.append(preprocess).append(fetch)

下载管线

force

force: boolean = !!EDITOR

是否强制加载资源, 如果为 true ,加载资源将会忽略报错

generalImportBase

generalImportBase: string = ""

generalNativeBase

generalNativeBase: string = ""

loadPipe

loadPipe: IPipe = load

packManager

packManager: PackManager = packManager

管理所有合并后的资源

parser

parser: Parser = parser

管理所有解析任务

pipeline

pipeline: Pipeline = pipeline.append(preprocess).append(load)

正常加载管线

preprocessPipe

preprocessPipe: IPipe = preprocess

presets

presets: {} = presets

可选参数的预设集

Type declaration

references

references: null | Cache<any[]> = references

transformPipeline

transformPipeline: Pipeline = transformPipeline.append(parse).append(combine)

Url 转换器

utils

utils: "D:/workCode/editor-3d/resources/3d/engine/cocos/core/asset-manager/helper" = helper

一些有用的方法

Accessors

main

resources

  • get resources(): Bundle | null

Methods

getBundle

  • getBundle(name: string): Bundle | null
  • 获取已加载的分包

    example

    // ${project}/assets/test1 cc.assetManager.getBundle('test1'); cc.assetManager.getBundle('resources');

    Parameters

    • name: string

      The name of bundle

    Returns Bundle | null

    • The loaded bundle

init

loadAny

  • loadAny(requests: Request, options: IOptions | null, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData | null): void
  • loadAny(requests: Request, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData | null): void
  • loadAny(requests: Request, options: IOptions | null, onComplete?: CompleteCallbackWithData | null): void
  • loadAny<T>(requests: string, onComplete?: CompleteCallbackWithData<T> | null): void
  • loadAny<T>(requests: string[], onComplete?: CompleteCallbackWithData<T[]> | null): void
  • loadAny(requests: Request, onComplete?: CompleteCallbackWithData | null): void
  • 通用加载资源接口,可传入进度回调以及完成回调,通过组合 requestoptions 参数,几乎可以实现和扩展所有想要的加载效果。非常建议 你使用更简单的API,例如 loadloadDir 等。options 中的自定义参数将会分发到 requests 的每一项中,如果request中已存在同名的 参数则以 requests 中为准,同时如果有其他依赖资源,则 options 中的参数会继续向依赖项中分发。request中的自定义参数都会以 options 形式传入加载流程中的 downloader, parser 的方法中, 你可以扩展 downloader, parser 收集参数完成想实现的效果。

    保留关键字: uuid, url, path, dir, scene, type, priority, preset, audioLoadMode, ext, bundle, onFileProgress, maxConcurrency, maxRequestsPerFrame, maxRetryCount, version, xhrResponseType, xhrWithCredentials, xhrMimeType, xhrTimeout, xhrHeader, reloadAsset, cacheAsset, cacheEnabled, 请不要使用这些字段为自定义参数!

    example

    cc.assetManager.loadAny({url: 'http://example.com/a.png'}, (err, img) => cc.log(img)); cc.assetManager.loadAny(['60sVXiTH1D/6Aft4MRt9VC'], (err, assets) => cc.log(assets)); cc.assetManager.loadAny([{ uuid: '0cbZa5Y71CTZAccaIFluuZ'}, {url: 'http://example.com/a.png'}], (err, assets) => cc.log(assets)); cc.assetManager.downloader.register('.asset', (url, options, onComplete) => { url += '?userName=' + options.userName + "&password=" + options.password; cc.assetManager.downloader.downloadFile(url, null, onComplete); }); cc.assetManager.parser.register('.asset', (file, options, onComplete) => { var json = JSON.parse(file); var skin = json[options.skin]; var model = json[options.model]; onComplete(null, {skin, model}); }); cc.assetManager.loadAny({ url: 'http://example.com/my.asset', skin: 'xxx', model: 'xxx', userName: 'xxx', password: 'xxx' });

    Parameters

    • requests: Request

      The request you want to load

    • options: IOptions | null

      Optional parameters

    • onProgress: ProgressCallback | null

      Callback invoked when progression change

    • onComplete: CompleteCallbackWithData | null

      Callback invoked when finish loading

    Returns void

  • Parameters

    • requests: Request
    • onProgress: ProgressCallback | null
    • onComplete: CompleteCallbackWithData | null

    Returns void

  • Parameters

    • requests: Request
    • options: IOptions | null
    • Optional onComplete: CompleteCallbackWithData | null

    Returns void

  • Type parameters

    Parameters

    • requests: string
    • Optional onComplete: CompleteCallbackWithData<T> | null

    Returns void

  • Type parameters

    Parameters

    • requests: string[]
    • Optional onComplete: CompleteCallbackWithData<T[]> | null

    Returns void

  • Parameters

    • requests: Request
    • Optional onComplete: CompleteCallbackWithData | null

    Returns void

loadBundle

  • loadBundle(nameOrUrl: string, options: IBundleOptions | null, onComplete?: CompleteCallbackWithData<Bundle> | null): void
  • loadBundle(nameOrUrl: string, onComplete?: CompleteCallbackWithData<Bundle> | null): void

loadRemote

  • loadRemote<T>(url: string, options: IRemoteOptions | null, onComplete?: CompleteCallbackWithData<T> | null): void
  • loadRemote<T>(url: string, onComplete?: CompleteCallbackWithData<T> | null): void

Private loadWithJson

  • loadWithJson<T>(json: Record<string, any>, options: IJsonAssetOptions | null, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<T> | null): void
  • loadWithJson<T>(json: Record<string, any>, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<T> | null): void
  • loadWithJson<T>(json: Record<string, any>, options: IJsonAssetOptions | null, onComplete?: CompleteCallbackWithData<T> | null): void
  • loadWithJson<T>(json: Record<string, any>, onComplete?: CompleteCallbackWithData<T> | null): void

postLoadNative

  • postLoadNative(asset: Asset, options: INativeAssetOptions | null, onComplete: CompleteCallbackNoData | null): void
  • postLoadNative(asset: Asset, onComplete?: CompleteCallbackNoData | null): void
  • 加载资源的原生文件,如果你勾选了'延迟加载资源'选项,你可能需要在使用资源之前调用此方法来加载原生文件

    example

    cc.assetManager.postLoadNative(texture, (err) => console.log(err));

    Parameters

    • asset: Asset

      The asset

    • options: INativeAssetOptions | null

      Some optional parameters

    • onComplete: CompleteCallbackNoData | null

      Callback invoked when finish loading

    Returns void

  • Parameters

    • asset: Asset
    • Optional onComplete: CompleteCallbackNoData | null

    Returns void

preloadAny

  • preloadAny(requests: Request, options: IOptions | null, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<RequestItem[]> | null): void
  • preloadAny(requests: Request, onProgress: ProgressCallback | null, onComplete: CompleteCallbackWithData<RequestItem[]> | null): void
  • preloadAny(requests: Request, options: IOptions | null, onComplete?: CompleteCallbackWithData<RequestItem[]> | null): void
  • preloadAny(requests: Request, onComplete?: CompleteCallbackWithData<RequestItem[]> | null): void
  • 通用预加载资源接口,可传入进度回调以及完成回调,非常建议你使用更简单的 API ,例如 preloadRes, preloadResDir 等。preloadAnyloadAny 几乎一样,区别在于 preloadAny 只会下载资源,不会去解析资源,你需要调用 cc.assetManager.loadAny(preloadTask) 来完成资源加载。

    example

    cc.assetManager.preloadAny('0cbZa5Y71CTZAccaIFluuZ', (err) => cc.assetManager.loadAny('0cbZa5Y71CTZAccaIFluuZ'));

    Parameters

    • requests: Request

      The request you want to preload

    • options: IOptions | null

      Optional parameters

    • onProgress: ProgressCallback | null

      Callback invoked when progression change

    • onComplete: CompleteCallbackWithData<RequestItem[]> | null

      Callback invoked when finish preloading

    Returns void

  • Parameters

    • requests: Request
    • onProgress: ProgressCallback | null
    • onComplete: CompleteCallbackWithData<RequestItem[]> | null

    Returns void

  • Parameters

    • requests: Request
    • options: IOptions | null
    • Optional onComplete: CompleteCallbackWithData<RequestItem[]> | null

    Returns void

  • Parameters

    • requests: Request
    • Optional onComplete: CompleteCallbackWithData<RequestItem[]> | null

    Returns void

releaseAll

  • releaseAll(): void

releaseAsset

  • releaseAsset(asset: Asset): void
  • 释放资源以及其依赖资源, 这个方法不仅会从 assetManager 中删除资源的缓存引用,还会清理它的资源内容。 比如说,当你释放一个 texture 资源,这个 texture 和它的 gl 贴图数据都会被释放。 注意,这个函数可能会导致资源贴图或资源所依赖的贴图不可用,如果场景中存在节点仍然依赖同样的贴图,它们可能会变黑并报 GL 错误。

    example

    // release a texture which is no longer need cc.assetManager.releaseAsset(texture);

    Parameters

    • asset: Asset

      The asset to be released

    Returns void

removeBundle

  • removeBundle(bundle: Bundle): void
  • 移除此包, 注意:这个包内的资源不会自动释放, 如果需要的话你可以在摧毁之前手动调用 {{#crossLink "Bundle/releaseAll:method"}}{{/crossLink}} 进行释放

    typescript

    removeBundle(bundle: cc.AssetManager.Bundle): void

    Parameters

    • bundle: Bundle

      The bundle to be removed

    Returns void

Generated using TypeDoc