Laya商业级3d实战_013数据持久化和位图字体

laya商业级3d游戏开发

本节目标:实现分数系统

知识点:
数值持久化,位图字体

新建game.scene摆好UI

在这里插入图片描述

分数采用了位图字体
资源路径
鱼图片 UISpritesheet\FishboneIcone.png
位图字体图片Textrue\numwhileOutLine.png

fontClip var 设置为fishfont

在这里插入图片描述

位图字体的制作:
Btmfont制作
https://ldc2.layabox.com/doc/?nav=zh-ts-1-2-5

我们团队实际中往往就更改下图片,让美术对好位置即可

坑位解说-为什么要使用位图字体?
位图字体的好处,可以避免BUG!
少部分机型以及没和laya合作的渠道
使用text或者label会有如下情况
在这里插入图片描述
平台反馈
在这里插入图片描述
Bug的出现对于审核和上线都非常不利,所以尽量避免

views新建GameView.ts,并挂到runtime
export default class GameView extends BaseView {
constructor() {
super();
}

public static instance: GameView;
fishfont: Laya.FontClip;
onAwake() {
    super.onAwake()
    GameView.instance = this;
}

}

为了便于流程控制,新建ui控制类,方便管理

Ui管理器
新建manager/ViewMgr.ts
export default class ViewMgr {
private static minstance: ViewMgr

    public static get instance() {

        if (ViewMgr.minstance == null) ViewMgr.minstance = new ViewMgr();
        return ViewMgr.minstance;
    }

    public OpenGame(callder, callbackFunc: Function) {
        Laya.Scene.open(SceneType.Game, false, Laya.Handler.create(callder, view => {
            let viewtype = view as GameView;
            //加载完成回调
            if (callbackFunc != null)
                callbackFunc.apply(callder);
        }));
    }

}

Script目录下新建other目录
新建SceneType.ts

/**场景类型 */
const enum SceneType {

/**首页 */
Home = "views/home.scene",
/**游戏页 */
Game = "views/game.scene",
/**复活页 */
Over = "views/over.scene",
//读取页
loading = "views/loadind.scene",
tipsFloat = "views/tipsFloat.scene",
//模拟广告
devad = "views/devad.scene"

}

Gamesample.ts

public static StartGame() {

    ViewMgr.instance.OpenGame(this, (view) => {

        SceneManager.LoadSceneByName('Game', this, this.OnGameSceneLoadOk);
    })
    
}

Game.ts
onAwake() {

//分数系统
this.fishCount = PlayerPrefs.GetInt(‘fish’, 0);
GameView.instance.fishfont.value = this.fishCount.toString();
}

//猫吃到鱼时+1且更新UI
playerEatFish() {
this.fishCount += 1;
GameView.instance.fishfont.value = this.fishCount.toString();
}

onDestroy() {
    //游戏结束后才保存数据,频繁写入数据很消耗性能
    PlayerPrefs.SetInt('fish', this.fishCount);
}

PlayerPrefs数据存取模块封装了什么?

迎合U3D开发者习惯,抹平各大平台API执行结果的差异化
private static GetValueNum(value_name: string, defaul: number): number {
//原型方法
var jsonData = Laya.LocalStorage.getItem(value_name);
//oppo null key == null
//vivo null key ==’’
if (jsonData == null || jsonData == ‘’)
return defaul;
var d = Number(jsonData);
return d;
}

Player.ts
OnCollisionEnter(source: AABBShape, target: AABBShape) {

console.log('OnCollisionEnter', target.mask);

if (target.mask == CollisionMask.Fish) {
  target.gameObject.active = false;
 //猫吃到鱼时+1且更新UI
  Game.instance.playerEatFish();

Game.scene 添加runitme
在这里插入图片描述

F8 ->ctrl+12 ->f5

在这里插入图片描述

总结:
使用PlayerPrefs进行数据读写
分数显示使用位图字体,加强对平台的兼容性
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程之力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值