123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import { Asset, assetManager, ImageAsset, SpriteFrame, Texture2D, _decorator } from 'cc';
- import { DEV } from 'cc/env';
- import { AutoBind } from '../common/AutoBind';
- import { StringUtil } from '../util/StringUtil';
- import { LoadQueue } from './LoadQueue';
- import { resLoader } from './ResLoader';
- const { ccclass, menu, property } = _decorator;
- @ccclass('ResKeeper')
- @menu('私有组件/ResKeeper')
- export class ResKeeper extends AutoBind {
- @property({ displayName: "启用分帧加载", tooltip: DEV && "该组件加载资源是否使用分帧加载" })
- private frame_load = false;
- @property({ displayName: "加载优先级", slide: true, max: 10, min: 0, step: 1, visible() { return this.frame_load == true; }, tooltip: DEV && "分帧加载优先级,优先级越大越先加载" })
- private load_priority = 0;
-
- private _res_cache = new Map<string, Asset>();
- private _uuid_sign = StringUtil.getUUID(12);
- private _uuid_idx = 0;
- protected onDestroy() {
- this.release();
- }
-
-
-
-
-
-
-
-
-
-
-
-
- protected load(bundle: string, url: string, type: typeof Asset, completed: (res: Asset, uuid?: string) => void) {
- let callback = (error: Error, res: Asset) => {
- if (error) {
- console.error(error);
- } else {
- if (this.isValid) {
- let uuid = this._addResCache(res);
- completed && completed(res, uuid);
- } else {
-
- }
- }
- };
- if (this.frame_load) {
- LoadQueue.pushLoad(this.load_priority, bundle, url, type, callback, this);
- } else {
- resLoader.load(bundle, url, type, callback);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private _addResCache(res: Asset) {
- ++this._uuid_idx;
- let uuid = this.node.uuid + "_<" + this._uuid_sign + ">_[" + this._uuid_idx + "]";
- res.addRef();
- this._res_cache.set(uuid, res);
- return uuid;
- }
- release() {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this._res_cache.forEach((res) => {
- res?.decRef();
- res = null;
- });
- this._res_cache.clear();
- }
- }
|