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
js 复制代码
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() {

    }
}

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

js 复制代码
this.addComponent(Main);
  1. 在Main.ts的onload里加载fgui界面:
js 复制代码
	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中切换场景
js 复制代码
private _onStartGame() {
    this.node.emit("scene_replace", "Game");
    this.destroy();
}

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

  1. Game.ts脚本的onLoad
js 复制代码
    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
js 复制代码
    private _onClickBack(evt: Event): void {
        fgui.GRoot.inst.removeChildren(0, -1, true);
        this.node.emit("scene_replace", "Main");
        this.destroy();
    }
相关推荐
yuanpan12 小时前
认识跨平台UI框架Flutter和MAUI区别,如何选。
flutter·ui·maui
EndingCoder14 小时前
Electron 高级 UI:集成 React 或 Vue.js
react.js·ui·electron·前端框架
我命由我123451 天前
Word - Word 的 5 种视图(页面视图、阅读视图、Web 版式视图、大纲视图、草稿视图)
ui·word·excel·photoshop·表格·ps·美工
CodingCos1 天前
【芯片设计-信号完整性 SI 学习 1.1.1 -- Unit Interval,比特周期】
学习·ui·si 比特周期
开开心心loky1 天前
[iOS] ViewController 的生命周期
macos·ui·ios·objective-c·cocoa
我命由我123452 天前
Excel 表格 - Excel 减少干扰、专注于内容的查看方式
学习·ui·excel·photoshop·表格·ps·美工
我命由我123452 天前
Excel 表格 - Excel 单元格添加边框
学习·ui·excel·课程设计·photoshop·ps·美工
伽蓝_游戏2 天前
UGUI源码剖析(15):Slider的运行时逻辑与编辑器实现
游戏·ui·unity·性能优化·c#·游戏引擎·.net
John_ToDebug2 天前
定制 ResourceBundle 的实现与 DuiLib 思想在 Chromium 架构下的应用解析
c++·chrome·ui
萌新小码农‍2 天前
Java分页 Element—UI
java·开发语言·ui