参考代码来自https://forum.cocos.org/t/topic/162400/26,感谢奉献,主要是有时候定制引擎太麻烦了
import * as cc from ‘cc’;
cc.game.once(cc.Game.EVENT_ENGINE_INITED, function () {
cc.js.mixin(cc.Canvas.prototype, {
__preload(): void {
// Stretch to matched size during the scene initialization
this.getComponent(cc.Widget)?.updateAlignment();
this._onResizeCamera();
// In Editor dont need resized camera when scene window resize
cc.view.on(‘canvas-resize’, this._thisOnCameraResized, this);
cc.view.on(‘design-resolution-changed’, this._thisOnCameraResized, this);
},
onDestroy(): void {
super.onDestroy();
cc.view.off('canvas-resize', this._thisOnCameraResized, this);
cc.view.off('design-resolution-changed', this._thisOnCameraResized, this);
},
});
});