import { _decorator, assetManager, Camera, Canvas, Component, director, DynamicAtlasManager, instantiate, macro, Node, sys, tween, UIOpacity, Vec3 } from "cc"; import { CommonConfig } from "../Config/CommonConfig"; import ResManager from "../Manager/ResManager"; import Utils from "./Utils"; const { ccclass, property, executeInEditMode, disallowMultiple } = _decorator; //游戏根目录 每个场景都要加一下 @ccclass // @executeInEditMode @disallowMultiple export default class GameRootUI extends Component { public static I: GameRootUI = null; //当前场景摄像机 private static _MainCamera: Camera = null; public static get MainCamera(): Camera { return GameRootUI._MainCamera } public static set MainCamera(value: Camera){ GameRootUI._MainCamera = value } public LayerNodeGroups = {} //界面组 @property(Node) UIRoot: Node = null @property(Node) UILayerMod: Node = null @property public DefaultUI: string = "" //默认打开的界面 onLoad() { GameRootUI.I = this; } start() { //创建界面层 for (const key in CommonConfig.UILayerGroup) { if (isNaN(Number(key))) { let newNode = instantiate(this.UILayerMod); newNode.name = key this.UIRoot.addChild(newNode) newNode.setPosition(Vec3.ZERO) newNode.setScale(Vec3.ONE) let l_idx = CommonConfig.UILayerGroup[key] newNode.setSiblingIndex(Number(l_idx)) this.LayerNodeGroups[key] = newNode } } //默认打开的界面 if (this.DefaultUI.length > 0) { ResManager.I.loadSubpackagePrefab(this.DefaultUI, (res)=>{ let viewSP = res.getComponent(res.name); viewSP && viewSP.openUIData && viewSP.openUIData(null); this.AddUIToLayer(res, CommonConfig.UILayerGroup.Layer_ui1); }) } } protected update(dt: number): void { GameRootUI.I.CheckDisableOperTime(dt) } //手动创建时需要调用一下初始化 public InitByCreate(_UIRoot: Node, _UILayerMod: Node, _DefaultUI: string) { this.UIRoot = _UIRoot this.UILayerMod = _UILayerMod this.DefaultUI = _DefaultUI } //添加一个界面到某个层上 public AddUIToLayer(ui:Node, e_layer:CommonConfig.UILayerGroup, zorder:number = 0){ if (ui == null) return var n_layer = CommonConfig.UILayerGroup[e_layer] if (this.LayerNodeGroups[n_layer] != null){ this.LayerNodeGroups[n_layer].addChild(ui) ui.setSiblingIndex(zorder) } } //禁用操作界面---------------------------------------------------------------------------- private static DisableOperUI: Node = null; private static DisableOperSwt: boolean = false; private static DisableOperDura: number = 0; private static DisableOperTime: number = 0; public static InitDisableOperUI(isshow: boolean) { if (GameRootUI.DisableOperUI == null){ ResManager.I.loadSubpackagePrefab("UI/Cross/DisableOper_UI", (res:Node)=>{ if (!GameRootUI.DisableOperUI) { GameRootUI.DisableOperUI = res director.getScene().addChild(res); director.addPersistRootNode(res); res.setSiblingIndex(9999) let croot = GameRootUI.DisableOperUI.getChildByName("croot") croot.active = false } if (isshow) { GameRootUI.DisableOper(GameRootUI.DisableOperDura) } }) } } /**开启禁用操作 * @param dura 禁用时间 单位秒 */ public static DisableOper(dura: number) { if (GameRootUI.DisableOperDura < dura){ GameRootUI.DisableOperDura = dura } if (GameRootUI.DisableOperUI == null){ GameRootUI.InitDisableOperUI(true) return } Utils.Log("禁用操作 开启") GameRootUI.DisableOperSwt = true let croot = GameRootUI.DisableOperUI.getChildByName("croot") croot.active = true } /**关闭禁用操作 */ public static EnableOper() { GameRootUI.DisableOperSwt = false GameRootUI.DisableOperDura = 0 GameRootUI.DisableOperTime = 0 if (GameRootUI.DisableOperUI){ let croot = GameRootUI.DisableOperUI.getChildByName("croot") croot.active = false } Utils.Log("禁用操作 关闭") } private CheckDisableOperTime(dt: number) { if (!GameRootUI.DisableOperSwt) return GameRootUI.DisableOperTime += dt; if (GameRootUI.DisableOperTime >= GameRootUI.DisableOperDura){ GameRootUI.EnableOper(); } } //过场界面---------------------------------------------------------------------------- public static CrossUI: Node = null; public static InitCrossUI(isshow: boolean, cb = null) { if (GameRootUI.CrossUI == null){ ResManager.I.loadSubpackagePrefab("UI/Cross/Cross_UI", (res:Node)=>{ if (!GameRootUI.CrossUI) { GameRootUI.CrossUI = res director.getScene().addChild(res); director.addPersistRootNode(res); res.setSiblingIndex(9999) let croot = GameRootUI.CrossUI.getChildByName("croot") croot.active = false } if (isshow) { GameRootUI.ShowCrossUI(cb) } }) } } public static ShowCrossUI(cb = null) { // if (true) { // return // } if (GameRootUI.CrossUI == null){ GameRootUI.InitCrossUI(true) return } // //设置摄像机 // let canvas = GameRootUI.CrossUI.getComponent(Canvas) // canvas.cameraComponent = GameRootUI.MainCamera //自动隐藏 // Utils.Log("Cross 显示切换界面") let croot = GameRootUI.CrossUI.getChildByName("croot") croot.active = true let opa = croot.getComponent(UIOpacity) opa.opacity = 255 tween(opa) .delay(0.3) .to(0.3, {opacity: 0}) .call(()=>{ croot.active = false // Utils.Log("Cross 隐藏切换界面") }) .start() } }