//Laya自适应主要是通过设置scaleMode=showall,并获取屏幕的DPR值来判断比例并进行偏移
//微信自适应
if (Laya.Browser.onWeiXin && Laya.Browser.onMiniGame) {
let systemInfo = wx.getSystemInfoSync();
let screenWidth = systemInfo.screenWidth;
let screenHeight = systemInfo.screenHeight;
//@ts-ignore
let safeArea = systemInfo.safeArea;
if (screenHeight / screenWidth >= 2) {
console.log(safeArea.top);
if(safeArea.top > 40){
Laya.stage.scaleMode = 'showall';
let topSafeHeight = safeArea.top;
if(Laya.Browser.pixelRatio == 2){
AppGame.stage.pos(0, topSafeHeight * (screenHeight / screenWidth) - (screenHeight - safeArea.bottom));
Laya.Browser.window['safeArea'] = topSafeHeight * (screenHeight / screenWidth) - (screenHeight - safeArea.bottom);
}else{
AppGame.stage.pos(0, topSafeHeight * (screenHeight / screenWidth));
Laya.Browser.window['safeArea'] = topSafeHeight * (screenHeight / screenWidth) - (screenHeight - safeArea.bottom);
}
}
}
}
//ios需要先获取safeAreaHeight来判断是否是刘海屏
//ios自适应
if(Laya.Browser.window.conch && Laya.Browser.onIOS){
//@ts-ignore
let bridge = PlatformClass.createClass("iOSUtil");//创建脚步代理 iOSUtil是自己在xcode写的脚步
let safeAreaHeight = bridge.call("safeAreaHeight");
if(safeAreaHeight){
let safeTop = parseInt(bridge.call("getSafeTop"));
let safeBottom = parseInt(bridge.call("getSafeBottom"));
let screenWidth = parseInt(bridge.call("getScreenWidth"));
let screenHeight = parseInt(bridge.call("getScreenHeight"));
let DPR = parseInt(bridge.call("getDPR"));
if(safeTop && safeTop > 40){
if(DPR == 2){
AppGame.stage.pos(0, safeTop * (screenHeight / screenWidth));
Laya.Browser.window['safeArea'] = Math.abs((safeTop * (screenHeight / screenWidth) - (screenHeight - safeBottom)) / 10);
}else{
AppGame.stage.pos(0, safeTop * (screenHeight / screenWidth));
Laya.Browser.window['safeArea'] = Math.abs((safeTop * (screenHeight / screenWidth) - (screenHeight - safeBottom)) / 10);
}
}
}
}
//android原生会自动自适应
Laya 屏幕自适应
于 2023-10-26 09:58:02 首次发布