CocosCreator UI管理

如下图所示: Canvas下有空物体 UIRoot,eUI, mUI, eUI用来显示常驻UI,mUI用来显示弹框UI.  Canvas绑定rukou.ts

 

BasePanel.ts

const {ccclass, property} = cc._decorator;

@ccclass
export default abstract  class BasePanel extends cc.Component {

    @property(cc.Node)
    mask:cc.Node = null;
    @property(cc.Boolean)
    modal:boolean = false;

    protected hideFun = null;

    abstract init():any;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    enter(showCb = null, hideCb = null)
    {
        this.node.active = true;
        if(showCb != null){
            showCb();
        }
        this.hideFun = hideCb;
    }

    exit()
    {
        if(this.hideFun != null){
            this.hideFun();
            this.hideFun = null;
        }
        this.node.active = false;
    }

    isModal()
    {
        return this.modal;
    }

    onClose(event:TouchEvent, customEventData:any)
    {
        this.exit();
    }

    useEffect()
    {
        this.node.setScale(0.5,0.5);
        let scaleOut = cc.scaleTo(0.2,1.1,1.1).easing(cc.easeCubicActionInOut());
        let scaleIn1 = cc.scaleTo(0.1,0.9,0.9).easing(cc.easeCubicActionIn());
        let scaleIn2 = cc.scaleTo(0.1,1.0,1.0).easing(cc.easeCubicActionIn());
        this.node.runAction(cc.sequence(scaleOut, scaleIn1, scaleIn2));
    }
}

Singleton.ts

const {ccclass, property} = cc._decorator;

@ccclass
export default class Singleton{
    
    protected static instance:Singleton;

    protected static GetInstance():Singleton
    {
        if(!this.instance)
        {
            this.instance = new Singleton();
        }
        return this.instance;
    }
}

UIMgr.ts

import Singleton from "./Singleton";
import BasePanel from "./BasePanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UIMgr extends Singleton {

    UIRoot:cc.Node = null;
    eUIRoot:cc.Node = null;
    mUIRoot:cc.Node = null;

    private curPanel:BasePanel = null;

    allEUIs:{[key:string]:any} = {};
    allMUIs:{[key:string]:any} = {};

    protected static instance:UIMgr;
    public static GetInstance():UIMgr
    {
        if(!this.instance)
        {
            this.instance = new UIMgr;
        }
        return this.instance;
    }

    init()
    {
        this.UIRoot= cc.find("Canvas").getChildByName("UIRoot");
        this.eUIRoot = this.UIRoot.getChildByName("eUI");
        this.mUIRoot = this.UIRoot.getChildByName("mUI");
    }

    public showUI(uiName:string, params = null)
    {
        let self= this;
        let uiNode = self.eUIRoot.getChildByName(uiName);
        if(uiNode == null)
        {
            cc.loader.loadRes("Prefab/ui/" + uiName, cc.Prefab, function(err, prefab){
                uiNode = cc.instantiate(prefab);
                uiNode.position = cc.Vec2.ZERO;
                self.eUIRoot.addChild(uiNode);
                let nextPanel = uiNode.getComponent(BasePanel);
                if(nextPanel != null)
                {
                    self.curPanel = nextPanel;
                    self.curPanel.enter(params);        
                }
            });
        }
        else
        {
            let nextPanel = uiNode.getComponent(BasePanel);
            self.curPanel = nextPanel;
            self.curPanel.enter(params);
        }
        return uiNode;
    }


    public showDialog(uiName, params)
    {
        let self = this;
        let uiNode = self.eUIRoot.getChildByName(uiName);
        if(uiNode == null)
        {
            cc.loader.loadRes("Prefab/ui/" + uiName, cc.Prefab, function(err, prefab){
                uiNode = cc.instantiate(prefab);
                uiNode.position = cc.Vec2.ZERO;
                self.mUIRoot.addChild(uiNode);
                let nextPanel = uiNode.getComponent(BasePanel);
                if(nextPanel != null)
                {
                    if(nextPanel.node.name == "xx")
                    {
                        nextPanel.node.zIndex = 2;
                    }
                    else{
                        nextPanel.node.zIndex = 1;
                    }
                    self.curPanel = nextPanel;
                    self.curPanel.enter(params);
                }  
            });
        }
        else
        {
            let nextPanel = uiNode.getComponent(BasePanel);
            self.curPanel = nextPanel;
            self.curPanel.enter(params);
        }
    }

    public getUI(uiName:string)
    {
        let self = this;
        let uiNode = self.eUIRoot.getChildByName(uiName);
        if(uiNode == null)
        {
            uiNode = self.mUIRoot.getChildByName(uiName);
            if(uiNode == null)
            {
                return null;
            }
        }
        return uiNode;
    }
}

 

简单调用:

ruou.ts

UIMgr.GetInstance().init();

UIMgr.GetInstance().showUI("HallPanel"); //HallPanel为预制体,绑定HallPanel.ts 继承BasePanel

HallPanel.ts

import BasePanel from "./BasePanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class HallPanel extends BasePanel {

    

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    init(){}

    start () {

    }

    enter()
    {
        super.enter();
        console.log("打开HallPanel~");
    }

    // update (dt) {}
}

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CocosCreator是一种用于游戏开发的跨平台游戏引擎框架。它提供了一个完整的开发环境,使开发者能够轻松地创建游戏的各个方面,包括游戏逻辑、场景管理、动画、粒子效果、碰撞检测、图形渲染等等。 CocosCreator的主要特点之一是跨平台支持,开发者可以使用它来创建同时适配多个平台的游戏,如Web、iOS、Android等。这意味着开发者只需要编写一次代码,就能在不同平台上运行游戏,大大提高了开发效率。 此外,CocosCreator还内置了一些强大的工具和组件,使开发者能够更快速和简单地创建游戏。例如,它提供了一个所见即所得的场景编辑器,使开发者能够轻松地创建和管理游戏场景。还有动画编辑器,用于创建和编辑游戏中的动画效果。此外,CocosCreator还提供了丰富的UI组件和预制件系统,使开发者能够快速构建复杂的界面和游戏对象。 CocosCreator还支持脚本编写,开发者可以使用JavaScript或TypeScript来编写游戏逻辑。这使得开发者能够以更灵活和高效的方式实现各种游戏功能。同时,CocosCreator还提供了一套完整的API文档和示例,方便开发者学习和使用框架。 总之,CocosCreator是一个功能强大且易于使用的游戏开发框架。它通过跨平台支持、强大的编辑工具和组件、脚本编写等特性,使开发者能够快速地开发高质量的游戏。无论是初学者还是有经验的开发者,都可以通过CocosCreator实现自己理想中的游戏作品。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值