laya 简易图片资源管理

相比如laya自带的资源组管理,个人感觉这个更方便,引用为0后自动移除资源。

话不多说,直接上代码。
/**

  • 自动释放资源图片类

  • addbuy zhuxl 2021/8/3
    */
    export default class ZAutoImage extends Laya.Image{
    constructor() {
    super()
    }
    set skin(url:string) {
    if (this._skin == url) return;
    this._skin = url;
    if (url) {
    this.off(Laya.Event.DISPLAY, this, this.onDisplay);
    this.off(Laya.Event.UNDISPLAY, this, this.onUnDisplay);
    this.on(Laya.Event.DISPLAY, this, this.onDisplay);
    this.on(Laya.Event.UNDISPLAY, this, this.onUnDisplay);
    } else {
    this.off(Laya.Event.DISPLAY, this, this.onDisplay);
    this.off(Laya.Event.UNDISPLAY, this, this.onUnDisplay);
    }
    this.checkSkin()
    }
    get skin() {
    return this._skin
    }
    onDisplay(e) {
    this.checkSkin()
    }
    onUnDisplay(e) {
    this.checkSkin()
    }

    private _resPack:ZAutoLoad = null;
    checkSkin() {
    if (this.displayedInStage && this._skin) {
    this._resPack = ZAutoLoad.getRes(this._skin, this, this.setSkin, this._resPack)
    } else {
    ZAutoLoad.delRes(this._resPack, this);
    this.source = null;
    this._resPack = null
    }
    }
    setSkin() {
    this.source = Laya.Loader.getRes(this._skin);
    this.onCompResize()
    }
    }

/**

  • 基础加载资源 自动释放资源

  • addbuy zhuxl 2021/8/3
    */
    export default class ZAutoLoad{
    constructor(url) {
    this.count = 0;
    this.completes = [];
    this._url = url;
    Laya.loader.load(this._url, Laya.Handler.create(this, this.onLoaded))
    }
    private count:number;
    private completes:{caller:Function,callback:any}[];
    private _url:string;

    static _packs = {};
    static _awaitClearT = {};
    static _start = false;

    onLoaded(e) {
    if (!e) {}
    while (this.completes.length) {
    var t = this.completes.pop();
    var _callBack:Function = t.callback;
    var s = t.caller
    _callBack.call(s, this._url)
    }
    this.completes = null
    }
    clear() {
    if (Laya.Loader.getRes(this._url)) {
    Laya.Loader.clearRes(this._url)
    } else {
    Laya.loader.cancelLoadByUrl(this._url)
    }
    }
    static getRes(url:string, caller:any, callBack:Function, s:ZAutoLoad) {
    if (this._start == false) {
    this._start = true;
    Laya.timer.loop(2e3, this, this.clearRes)
    }
    this.delRes(s, caller);
    var r = this._packs[url];
    if (!r) {
    r = new ZAutoLoad(url);
    this._packs[url] = r
    }
    if (r.completes) {
    r.completes.push({
    caller: caller,
    callback: callBack
    })
    } else {
    callBack.call(caller, url)
    }
    r.count++;
    if (this._awaitClearT[url]) {
    delete this._awaitClearT[url]
    }
    return r
    }
    static delRes(e:ZAutoLoad, caller:any) {
    if (e) {
    e.count–;
    if (e.completes) {
    for (var i = 0; i < e.completes.length; i++) {
    if (e.completes[i].caller == caller) {
    e.completes.splice(i, 1);
    break
    }
    }
    }
    if (e.count <= 0) {
    this._awaitClearT[e._url] = Laya.Browser.now()
    }
    }
    }
    static clearRes() {
    for (var e in this._awaitClearT) {
    var t = this._awaitClearT[e];
    let s;
    if (Laya.Browser.onMobile || Laya.Browser.window.conch || Laya.Browser.onSafari) {
    s = 1e3;
    } else {
    s = 3e4;
    }
    if (Laya.Browser.now() - t >= s) {
    this.clearOneRes(e);
    delete this._awaitClearT[e]
    }
    }
    }
    static clearOneRes(url) {
    var t = this._packs[url];
    t.clear();
    delete this._packs[url];
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值