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();
    }
相关推荐
ZC跨境爬虫6 小时前
跟着 MDN 学 HTML day_36:(深入理解 Comment 接口与 DOM 注释节点)
前端·javascript·ui·html·音视频·视频编解码
for_ever_love__8 小时前
UI学习:无限轮播视图
学习·ui·ios·objective-c
UnicornDev9 小时前
【Flutter x HarmonyOS 6】魔方计时APP——记录页面的UI设计
flutter·ui·华为·harmonyos·鸿蒙
秋雨梧桐叶落莳11 小时前
iOS——MVC架构学习
学习·ui·ios·架构·mvc·objective-c
weixin_4462608511 小时前
AI驱动的前沿前端技术栈深度解析:从模型能力到UI封装的完整生命周期
前端·人工智能·ui
ZC跨境爬虫11 小时前
跟着 MDN 学 HTML day_35:(深入解析 CharacterData 抽象接口)
java·前端·ui·html·edge浏览器·媒体
RReality12 小时前
【Unity Shader URP】视差贴图 实战教程
ui·平面·unity·游戏引擎·图形渲染·贴图
ZC跨境爬虫13 小时前
跟着 MDN 学 HTML day_37:(深入掌握 CustomEvent 自定义事件接口)
前端·javascript·ui·html·音视频
UXbot21 小时前
独立设计师UI设计工具推荐(2026):支持AI原型生成与代码导出的5款工具全面评价
前端·人工智能·低代码·ui·交互·产品经理·web app
代码的小搬运工1 天前
UITableView
开发语言·ui·ios·objective-c