50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { _decorator, Component, Node, ResolutionPolicy, screen, Screen, view } from 'cc';
|
|
import { WINDOWS } from 'cc/env';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('UIAdapt')
|
|
export class UIAdapt extends Component {
|
|
start() {
|
|
screen.on('window-resize', this.adaptScreenSize, this);
|
|
this.adaptScreenSize()
|
|
let self = this
|
|
this.scheduleOnce(() => {
|
|
self.adaptScreenSize()
|
|
}, 0.1)
|
|
}
|
|
|
|
adaptScreenSize() {
|
|
let screenWidth = view.getViewportRect().width
|
|
let screenHeight = view.getViewportRect().height
|
|
// let screenWidth = view.getVisibleSize().width;
|
|
// let screenHeight = view.getVisibleSize().height;
|
|
let screenRatio = screenHeight / screenWidth;
|
|
|
|
console.log("修改屏幕分辨率", screenRatio, view.getViewportRect(), view)
|
|
|
|
if (screenRatio > 18 / 9) {
|
|
// 适配屏幕高度
|
|
console.log("修改屏幕分辨率", "适配高度")
|
|
|
|
|
|
let designWidth = 1080;
|
|
let designHeight = screenHeight * (designWidth / screenWidth);
|
|
|
|
view.setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy.FIXED_WIDTH);
|
|
} else if (screenRatio > 16 / 9) {
|
|
// 适配屏幕宽度
|
|
console.log("修改屏幕分辨率", "适配屏幕宽度")
|
|
let designHeight = 1920;
|
|
let designWidth = screenWidth * (designHeight / screenHeight);
|
|
view.setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy.FIXED_HEIGHT);
|
|
} else {
|
|
console.log("修改屏幕分辨率", "适配屏幕宽高")
|
|
let designWidth = 1080;
|
|
let designHeight = screenHeight * (designWidth / screenWidth);
|
|
view.setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy.EXACT_FIT);
|
|
}
|
|
}
|
|
}
|
|
|
|
|