LayaBox进阶之UI管理器

自己动手写框架的话,UI管理器是最基础的一部分;

打开界底层是addChild打开的;

新建一个UIManager

1export class UIManager { 2 private mainContent: Laya.Sprite; 3 private scene: GameScence; 4 private uiList:any[]; 5 6 constructor() { 7 this.mainContent = new Laya.Sprite(); 8 9 this.uiList = []; 10 } 11 }

  mainContent 为默认添加到的层级,

GameScence 为UI管理器默认场景

uiList放所有加载进来的场景

第一步实现 打开界面 openWindow

1public openWindow(clazz: any, param?: { data?: any }) { 2 let view = new clazz() 3 this.setView(clazz, view); 4 this.mainContent.addChild(view); 5 }

  初始化传进来的场景类;

存进字典

添加到显示层

下面是缓存的过程 setView

1private setView(clazz: any, view:Laya.Node):void{ 2 let v: Laya.Node = this.getView(clazz); 3 if(!v){ 4 let uiData = {clazz: clazz, view: view}; 5 this.uiList.push(uiData); 6 } 7 }

  首先判断本地缓存有没有,有的话 不处理,没得话,创建,push到数组中

1private getView(clazz: any):Laya.Node{ 2 for(let i:number =0 ; i<this.uiList.length ; i++){ 3 let uiData = this.uiList[i]; 4 if(uiData.clazz == clazz){ 5 return uiData.view; 6 } 7 } 8 }

根据clazz名字获取本地缓存的场景类

下面是关闭场景,

1public closeWindow(clazz: any): void { 2 let v = this.getView(clazz); 3 let index: number = this.getViewIndex(clazz); 4 if(v){ 5 v.removeSelf(); 6 this.uiList.splice(index,1); 7 } 8 } 9 10private getViewIndex(clazz: any):number{ 11 for(let i:number =0 ; i<this.uiList.length ; i++){ 12 let uiData = this.uiList[i]; 13 if(uiData.clazz == clazz){ 14 return i; 15 } 16 } 17 return -1; 18 }

这就是最简单的打开和关闭;

还要有一个初始化UIManager的方法, 设置管理器的初始化场景

1public setGameScene(gameScene: GameScence): void { 2 this.scene = gameScene; 3 if (this.scene) { 4 this.scene.parent.addChild(this.mainContent); 5 } 6 } 7 8export let ui: UIManager = new UIManager(); 9window["ui"] = ui;

有时候通过open方法,或者UiManager打开一个场景后,场景里面的元素还不能正常获取;解决办法是

修改Node Sprite的原型, 那么,所有的场景被加载时,都会执行这些方法,

1import { LogUtil } from "../util/LogUtil"; 2 3/** 4* 为基类定义若干方法 5*/ 6export class PatchManager{} 7(function(){ 8let _proto:any; 9 10_proto = Laya.Scene.prototype; 11_proto.createView = function(view:Object){ 12if (view && !this._viewCreated) { 13this._viewCreated = true; 14Laya.SceneUtils.createByData(this, view); 15} 16 17this.onInit(); 18this.onShow(); 19 20Laya.timer.frameLoop(1, this, ()=>{ 21// console.info(this); 22this.onUpdate(); 23}); 24} 25 26/******************************************************************************** 27* Node 28********************************************************************************/ 29_proto = Laya.Node.prototype; 30 31_proto.onInit = function(){ 32} 33 34_proto.onShow = function(){ 35} 36 37_proto.onUpdate = function(){ 38} 39 40_proto.onDisable=function(){ 41this.onHide(); 42 } 43 44_proto.onHide = function(){ 45} 46})();

在场景中 直接写  onShow () {}   界面被加载完成后, 会自动调用这个方法、。

搞定。

点赞
收藏

评论区

加载中...

相关推荐

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

手把手教你用Python高仿一个任务管理器

大家好,我是Python进阶者。前言相信大家对任务管理器都不是很陌生了,CtrlAltDel即可打开,然后点击启动任务管理器,或者右击任务栏启动任务管理器即可启动任务管理器,启动之后界面如下:可以看出它列举出了一些重要的参数,比如进程数量,CPU使用率,物理内存,接下来我们就来一一列举出来。一、项目准备编辑器:sublimetext3模块:p

2018前端最火的web UI框架

1.AliceuiAliceui是支付宝的样式解决方案,是一套精选的基于spm生态圈的样式模块集合,是Arale的子集,也是一套模块化的样式命名和组织规范,是写CSS的更好方式。gitHub地址:https://github.com/aliceui/aliceui.github.io(https://www.osc

2018前端最火的web UI框架

1.AliceuiAliceui是支付宝的样式解决方案,是一套精选的基于spm生态圈的样式模块集合,是Arale的子集,也是一套模块化的样式命名和组织规范,是写CSS的更好方式。gitHub地址:https://github.com/aliceui/aliceui.github.io(https://www.osc

Autojs的intent使用

以打开淘宝app的搜索页面为例,先准备工具:Activity管理器下载地址:https://www.cr173.com/soft/824332.html备用地址:https://pan.baidu.com/s/1l2266vDbzp0hYcchkUC5jg提取码:qeve打开管理器!(https://oscimg.oschina

LayaBox进阶之UI管理器 - HelloWorld