Files
mp-xieyingeng/assets/scripts/utils/BackgroundScaler.ts
richarjiang 02a67909d6 init
2026-03-11 09:04:33 +08:00

28 lines
714 B
TypeScript

import { _decorator, Component, UITransform, view } from 'cc';
const { ccclass, menu, disallowMultiple } = _decorator;
@ccclass('BackgroundScaler')
@menu('Nexux/BackgroundScaler')
@disallowMultiple()
export class BackgroundScaler extends Component {
onLoad() {
const bg = this.node;
const uiTransform = bg.getComponent(UITransform);
const bgSize = uiTransform?.contentSize;
if (!bgSize) {
console.error('BackgroundScaler: bgSize is null');
return;
}
const winSize = view.getVisibleSize();
const scaleX = winSize.width / bgSize.width;
const scaleY = winSize.height / bgSize.height;
const scale = Math.max(scaleX, scaleY);
bg.setScale(scale, scale);
}
}