以前版本没有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;
}
}
});
}
}