init
This commit is contained in:
27
assets/scripts/utils/BackgroundScaler.ts
Normal file
27
assets/scripts/utils/BackgroundScaler.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user