From 2377040fef7dedf13996b0cf8d55dff8c0b3167f Mon Sep 17 00:00:00 2001 From: dgflash Date: Tue, 19 Nov 2024 11:32:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A7=BB=E5=8A=A8=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=AE=89=E5=85=A8=E5=8C=BA=E5=9F=9F=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/Root.ts | 8 ++- assets/core/gui/layer/LayerManager.ts | 70 +++++++++++++++------------ assets/module/config/GameConfig.ts | 5 ++ 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/assets/core/Root.ts b/assets/core/Root.ts index 231ea90..95c0b69 100644 --- a/assets/core/Root.ts +++ b/assets/core/Root.ts @@ -90,13 +90,17 @@ export class Root extends Component { oops.game = new GameManager(this.game); // 游戏界面管理 - oops.gui = new LayerManager(this.gui); + oops.gui = new LayerManager(); + oops.gui.mobileSafeArea = oops.config.game.mobileSafeArea; + //@ts-ignore + oops.gui.initLayer(this.gui); // 网络模块 oops.http.server = oops.config.game.httpServer; // Http 服务器地址 oops.http.timeout = oops.config.game.httpTimeout; // Http 请求超时时间 - game.frameRate = oops.config.game.frameRate; // 初始化每秒传输帧数 + // 初始化每秒传输帧数 + game.frameRate = oops.config.game.frameRate; this.enabled = true; this.init(); diff --git a/assets/core/gui/layer/LayerManager.ts b/assets/core/gui/layer/LayerManager.ts index 7341053..4096c9a 100644 --- a/assets/core/gui/layer/LayerManager.ts +++ b/assets/core/gui/layer/LayerManager.ts @@ -1,4 +1,4 @@ -import { Camera, Layers, Node, ResolutionPolicy, Widget, screen, view, warn } from "cc"; +import { Camera, Layers, Node, ResolutionPolicy, SafeArea, Widget, screen, view, warn } from "cc"; import { oops } from "../../Oops"; import { UICallbacks } from "./Defines"; import { DelegateComponent } from "./DelegateComponent"; @@ -86,20 +86,49 @@ export class LayerManager { windowAspectRatio: number = 0; /** 设计宽高比例 */ designAspectRatio: number = 0; + /** 是否开启移动设备安全区域适配 */ + mobileSafeArea: boolean = false; /** 界面层 */ - private readonly ui!: LayerUI; + private ui!: LayerUI; /** 弹窗层 */ - private readonly popup!: LayerPopUp; + private popup!: LayerPopUp; /** 只能弹出一个的弹窗 */ - private readonly dialog!: LayerDialog; + private dialog!: LayerDialog; /** 游戏系统提示弹窗 */ - private readonly system!: LayerDialog; + private system!: LayerDialog; /** 消息提示控制器,请使用show方法来显示 */ - private readonly notify!: LayerNotify; + private notify!: LayerNotify; /** UI配置 */ private configs: { [key: number]: UIConfig } = {}; + /** + * 初始化界面层 + * @param root 界面根节点 + */ + private initLayer(root: Node) { + this.root = root; + this.initScreenAdapter(); + this.camera = this.root.getComponentInChildren(Camera)!; + this.game = this.create_node(LayerType.Game); + + this.ui = new LayerUI(LayerType.UI); + this.popup = new LayerPopUp(LayerType.PopUp); + this.dialog = new LayerDialog(LayerType.Dialog); + this.system = new LayerDialog(LayerType.System); + this.notify = new LayerNotify(LayerType.Notify); + this.guide = this.create_node(LayerType.Guide); + + root.addChild(this.game); + root.addChild(this.ui); + root.addChild(this.popup); + root.addChild(this.dialog); + root.addChild(this.system); + root.addChild(this.notify); + root.addChild(this.guide); + } + + /** 初始化屏幕适配 */ private initScreenAdapter() { const drs = view.getDesignResolutionSize(); const ws = screen.windowSize; @@ -120,32 +149,11 @@ export class LayerManager { oops.log.logView("适配屏幕宽度", "【竖屏】"); } view.setDesignResolutionSize(finalW, finalH, ResolutionPolicy.UNKNOWN); - } - /** - * 构造函数 - * @param root 界面根节点 - */ - constructor(root: Node) { - this.root = root; - this.initScreenAdapter(); - this.camera = this.root.getComponentInChildren(Camera)!; - this.game = this.create_node(LayerType.Game); - - this.ui = new LayerUI(LayerType.UI); - this.popup = new LayerPopUp(LayerType.PopUp); - this.dialog = new LayerDialog(LayerType.Dialog); - this.system = new LayerDialog(LayerType.System); - this.notify = new LayerNotify(LayerType.Notify); - this.guide = this.create_node(LayerType.Guide); - - root.addChild(this.game); - root.addChild(this.ui); - root.addChild(this.popup); - root.addChild(this.dialog); - root.addChild(this.system); - root.addChild(this.notify); - root.addChild(this.guide); + if (this.mobileSafeArea) { + this.root.addComponent(SafeArea); + oops.log.logView("开启移动设备安全区域适配"); + } } /** diff --git a/assets/module/config/GameConfig.ts b/assets/module/config/GameConfig.ts index 6a0c9fd..af0a99c 100644 --- a/assets/module/config/GameConfig.ts +++ b/assets/module/config/GameConfig.ts @@ -77,6 +77,11 @@ export class GameConfig { return this._data.config.loadingTimeoutGui || 1000; } + /** 是否开启移动设备安全区域适配 */ + get mobileSafeArea(): boolean { + return this._data.config.mobileSafeArea || false; + } + private readonly _data: any = null; /** 游戏配置数据 */ get data(): any {