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();
    }
相关推荐
老马啸西风8 小时前
sensitive-word-admin v2.0.0 全新 ui 版本发布!vue+前后端分离
vue.js·ui·ai·nlp·github·word
csdn_aspnet1 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
課代表2 天前
Excel VBA 词频统计宏
ui·excel··vba·模块·字典
weixin_446260852 天前
提高工作效率的新选择[特殊字符]——Element Plus UI库
ui
沐雨潇竹2 天前
使用定时器监视当前PID 如果当前程序关闭 UI_Core.exe 也随之自动关闭实现方法
开发语言·qt·ui
Htht1112 天前
【Qt】之【Bug】点击按钮(ui->pushButton)触发非本类设置的槽函数
qt·ui·bug
CodeCraft Studio2 天前
报表控件stimulsoft教程:使用 JoinType 关系参数创建仪表盘
前端·ui
白熊1882 天前
【图像大模型】Stable Diffusion Web UI:深度解析与实战指南
ui·stable diffusion
OJAC近屿智能3 天前
英伟达发布Llama-Nemotron系列新模型,性能超越DeepSeek-R1
大数据·人工智能·ui·aigc·llama
UI设计兰亭妙微3 天前
未来设计新篇章!2025 年 UX/UI 设计趋势,技术与体验的全新结合!
ui·ux