当前位置: 首页 > news >正文

FairyGUI × Cocos Creator 3.x 场景切换

前言

前文提要:
FariyGUI × Cocos Creator 入门
FairyGUI × Cocos Creator 3.x 使用方式

个人demo:https://gitcode.net/qq_36286039/fgui_cocos_demo_dust

个人demo可能会更新其他代码,还请读者阅读本文内容,自行理解并实现。

官方demo:https://github.com/fairygui/FairyGUI-cocoscreator/tree/ccc3.0

场景切换

阅读一下FairyGUI官方给的Cocos Creator 3.x 的 demo,可以看见它是怎么场景切换的。

  1. 场景里挂一个常驻节点
    在这里插入图片描述
  2. 挂上一个脚本Entry.ts
import * as cc from 'cc';
const { ccclass, property } = cc._decorator;
import * as fgui from "fairygui-cc";
import Main from './Main';@ccclass
export default class Entry extends cc.Component {private _currentDemo: cc.Component = null!;onLoad() {fgui.GRoot.create();this.node.on("scene_replace", this._onDemoStart, this);this.addComponent(Main);}private _onDemoStart(demoStr: string) {let demo: cc.Component = this.addComponent(demoStr)!;this._currentDemo = demo;}start() {}
}

这一行代码帮助我们先切换到第一个场景:

this.addComponent(Main);
  1. 在Main.ts的onload里加载fgui界面:
	public onLoad() {cc.assetManager.loadBundle("UI", (err, res) => {fgui.UIPackage.loadPackage(res, "MainPkg", this._onUILoaded.bind(this));});}private _onUILoaded() {this._view = fgui.UIPackage.createObject("MainPkg", "Main").asCom;this._view.makeFullScreen();fgui.GRoot.inst.addChild(this._view);this.StartBtn = this._view.getChild("StartBtn");this._initBtnEvents();}

其中MainPkg对应fgui中的包名,Main对应包内的组件名,这些代码观察demo即可。

  1. 在Main中切换场景
private _onStartGame() {this.node.emit("scene_replace", "Game");this.destroy();
}

这段代码帮我们从Main脚本切换到Game脚本

  1. Game.ts脚本的onLoad
    public onLoad() {let UIBundle = null;cc.assetManager.loadBundle("UI", (err, res) => {UIBundle = res;fgui.UIPackage.loadPackage(UIBundle, "GamePkg", this._onUILoaded.bind(this));});}private _onUILoaded(err, pkg) {this._view = fgui.UIPackage.createObject("GamePkg", "Game").asCom;this._view.makeFullScreen();fgui.GRoot.inst.addChild(this._view);this.BackBtn = this._view.getChild("BackBtn");this.BackBtn.on(cc.Node.EventType.TOUCH_END, this._onClickBack, this);}
  1. 从Game返回到Main
    private _onClickBack(evt: Event): void {fgui.GRoot.inst.removeChildren(0, -1, true);this.node.emit("scene_replace", "Main");this.destroy();}
http://www.lryc.cn/news/305051.html

相关文章:

  • 【Java程序设计】【C00288】基于Springboot的篮球竞赛预约平台(有论文)
  • textbox文本框跨线程写入,扩展textobx控件
  • 【踩坑】PyTorch中指定GPU不生效和GPU编号不一致问题
  • 线性代数:向量、张量、矩阵和标量
  • WordPres Bricks Builder 前台RCE漏洞
  • 渗透测试—信息收集
  • 安卓adb调试备忘录
  • 【软件架构】01-架构的概述
  • Vue 图片轮播第三方库 介绍
  • 设置主从复制时发生报错Could not find first log file name in binary log index file‘;解决方案
  • React Context的使用方法
  • ElasticSearch索引数据备份与恢复
  • kubernetes日志收集 fluent-operator 动态索引名的实现
  • pip换源
  • 7.(数据结构)堆
  • AWS Elastic Beanstalk通过应用负载均衡配置https
  • AC自动机:文本搜索的加速器
  • 备战蓝桥杯---基础算法刷题1
  • 探索 Flutter 中的动画:使用 flutter_animate
  • 装机容量对光伏发电量的影响有多大?如何通过装机容量计算发电量?
  • 软考37-上午题-【数据库】-数据模型、数据库的三级模式和二级映像
  • 06 分频器设计
  • 力扣hot100题解(python版7-9题)
  • ECMAScript 6+ 新特性 ( 四 ) 迭代器 与 生成器
  • 【MySQL】事务的一致性究竟怎么理解?
  • 证件照(兼容H5,APP,小程序)
  • pytorch-textregression,中文文本回归实践,支持多值输出
  • go语言学而思【持续更新】
  • LVS-NAT之VMNET环境搭建
  • [TCP] TCP/IP 基础知识词典(2)