美文网首页
cocos creator老版本的使用addRef

cocos creator老版本的使用addRef

作者: Jey | 来源:发表于2021-07-22 10:27 被阅读0次

以前版本没有addRef/decRef的时候,需要我们手动管理cc.Asset的引用计数的时候,使用Object的defineProperties添加到它的原型

// 为cc.Asset注入引用计数的功能
    private static assetInit() {
        console.log('asset init');
        if (!Object.getOwnPropertyDescriptor(cc.Asset.prototype, 'addRef')) {
            Object.defineProperties(cc.Asset.prototype, {
                refDepends: {
                    configurable: true,
                    writable: true,
                    enumerable: false,
                    value: false,
                },
                refCount: {
                    configurable: true,
                    writable: true,
                    enumerable: false,
                    value: 0,
                },
                addRef: {
                    configurable: true,
                    writable: true,
                    value: function (): cc.Asset {
                        ++this.refCount;
                        return this;
                    }
                },
                decRef: {
                    configurable: true,
                    writable: true,
                    value: function (autoRelease = true): cc.Asset {
                        --this.refCount;
                        if (this.refCount <= 0 && autoRelease) {
                            ResManager.Instance.releaseAsset(this);
                        }
                        return this;
                    }
                }
            });
        }
    }

相关文章

网友评论

      本文标题:cocos creator老版本的使用addRef

      本文链接:https://www.haomeiwen.com/subject/jeeemltx.html