28 lines
714 B
TypeScript
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);
|
|
}
|
|
}
|