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();
    }
相关推荐
叹一曲当时只道是寻常25 分钟前
AI书签管理工具开发全记录(十三):TUI基本框架搭建
ui·go
海尔辛13 小时前
Unity UI 性能优化--Sprite 篇
ui·unity·性能优化
QQ6765800817 小时前
基于 PyTorch 的 VGG16 深度学习人脸识别检测系统的实现+ui界面
人工智能·pytorch·python·深度学习·ui·人脸识别
pop_xiaoli21 小时前
UI学习—cell的复用和自定义cell
学习·ui·ios
测试老哥2 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
步、步、为营2 天前
.net jwt实现
ui·.net
藏在歌词里2 天前
Axure-元件&流程图
ui·photoshop
孤蓬&听雨2 天前
Axure形状类组件图标库(共8套)
ui·产品经理·axure·产品设计·原型设计
生产队队长2 天前
项目练习:element ui 的icon放在button的右侧
开发语言·javascript·ui
小赖同学啊2 天前
Quick UI 组件加载到 Axure
ui·axure·photoshop