rp_11001/assets/Loading/scripts/LayoutAdapter.ts
TJH f46b37ec39
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 1m13s
消除时掉线可以重试或退出
2026-01-07 17:10:18 +08:00

124 lines
4.6 KiB
TypeScript

import { _decorator, Node, Component, screen, view, ResolutionPolicy, Sprite, sys, UITransform, Label, find } from 'cc';
import { LocalizedLabel } from './i18n/LocalizedLabel';
import { getLanguage } from './comm';
const { ccclass, property } = _decorator;
export let SWITCH_PROTRAIT_MODE = {
"da": "Skift venligst til portrættilstand",
"de": "Bitte wechseln Sie in den Portraitmodus",
"en": "Please switch to portrait mode",
"es": "Por favor, cambie al modo retrato",
"fi": "Vaihda muotokuvatilaan",
"fr": "Veuillez passer en mode portrait",
"id": "Silakan beralih ke mode potret",
"it": "Passare alla modalità verticale",
"ja": "ポートレートモードに切り替えてください",
"ko": "세로 모드로 전환해주세요",
"my": "ပုံတူမုဒ်သို့ ပြောင်းပါ။",
"nl": "Schakel over naar de portretmodus",
"pt": "Por favor, mude para o modo retrato",
"ro": "Treceți la modul portret",
"ru": "Пожалуйста, переключитесь в портретный режим",
"sv": "Växla till porträttläge",
"th": "กรุณาสลับเป็นโหมดแนวตั้ง",
"tr": "Lütfen portre moduna geçin",
"vi": "Vui lòng chuyển sang chế độ chân dung",
"zh": "請切換到縱向模式"
};
@ccclass('LayoutAdapter')
export class LayoutAdapter extends Component {
@property(Node)
BG_01: Node = null;
@property(Node)
Logo: Node = null;
@property(Node)
rotateNode: Node = null;
sprite: Sprite = null;
protected onLoad(): void {
this.sprite = this.BG_01.getComponent(Sprite);
this.rotateNode.active = false;
this.rotateNode.getChildByName('tips').active = false;
}
start(): void {
screen.on('window-resize', this.onWindowResize, this);
screen.on('orientation-change', this.onWindowResize, this);
this.onWindowResize();
this.rotateNode.getChildByName('tips').getComponent(Label).string = SWITCH_PROTRAIT_MODE[getLanguage()];
this.rotateNode.getChildByName('tips').active = true;
}
onWindowResize() {
const designSize = view.getDesignResolutionSize();
const screenSize0 = view.getVisibleSize(); // 先记录一份(如果你需要对比)
// 1) 依据宽高比切换策略
const screenRatio = screenSize0.width / screenSize0.height;
const designRatio = designSize.width / designSize.height;
view.setResolutionPolicy(
screenRatio > designRatio
? ResolutionPolicy.FIXED_HEIGHT // 横屏更宽:以高适配
: ResolutionPolicy.FIXED_WIDTH // 竖屏更窄:以宽适配
);
// 2) 下一帧再取"最终可视尺寸"来计算缩放
this.scheduleOnce(() => {
const size = view.getVisibleSize(); // 已更新后的正确值
const texW = this.sprite.spriteFrame.width;
const texH = this.sprite.spriteFrame.height;
const scale = Math.max(size.width / texW, size.height / texH);
this.BG_01.setScale(scale, scale); // 铺满并保持比例
if (this.Logo.isValid) {
this.changeLoge(size.height)
}
if (this.isMobile()) {
let isPortrait = size.width > size.height;
if (size.width > size.height) {
this.rotateNode.getChildByName('block').setScale(scale, scale);
this.rotateNode.active = true;
} else {
this.rotateNode.getChildByName('block').setScale(scale, scale);
this.rotateNode.active = false;
}
let web = find('Canvas/Mask/GameNode/webView')
if (web) web.active = !isPortrait
} else {
this.rotateNode.active = false;
}
}, 0);
}
changeLoge(texH) {
//记录背景当前大小
let height = texH / 2;
let num = (this.Logo.getComponent(UITransform).height) / 2;
let posY = -(height + 60 - num);
this.Logo.setPosition(0, posY, 0);
}
onDestroy() {
screen.off('window-resize', this.onWindowResize, this);
screen.off('orientation-change', this.onWindowResize, this);
}
isMobile(): boolean {
return (
sys.platform === sys.Platform.ANDROID ||
sys.platform === sys.Platform.IOS ||
sys.platform === sys.Platform.MOBILE_BROWSER ||
sys.platform === sys.Platform.UNKNOWN
);
}
}