In the past two years, the AI graphics technology has made great progress. It is also a fulfillment of a dream to remake the game types that have been played before.
In view of unity's commercial watermark and startup time, I decided to use Laya for development. At present, laya has been updated to version 3.0 or above, so use the current relatively new version.
After that, I will record some minor problems encountered in the development in this blog. After all, it's just a personal project, so update it as a post.
1. The life cycle of Laya
There is nothing to say about this. After understanding the life cycle of unity, the design is almost the same. When instantiating a class that inherits Laya, you can also see related functions:
/**
* 组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
*/
//onAwake(): void {}
/**
* 组件被启用后执行,例如节点被添加到舞台后
*/
//onEnable(): void {}
/**
* 组件被禁用时执行,例如从节点从舞台移除后
*/
//onDisable(): void {}
/**
* 第一次执行update之前执行,只会执行一次
*/
//onStart(): void {}
/**
* 手动调用节点销毁时执行
*/
//onDestroy(): void {
/**
* 每帧更新时执行,尽量不要在这里写大循环逻辑或者使用getComponent方法
*/
//onUpdate(): void {}
/**
* 每帧更新时执行,在update之后执行,尽量不要在这里写大循环逻辑或者使用getComponent方法
*/
//onLateUpdate(): void {}
/**
* 鼠标点击后执行。与交互相关的还有onMouseDown等十多个函数,具体请参阅文档。
*/
//onMouseClick(): void {}
2. Singleton of TS
Singleton is a common design pattern, and the traditional way of writing ts singleton is as follows:
export class UIProp {
private static instance: UIProp = null;
static getInstance() {
if (!UIProp.instance) {
UIProp.instance = new UIProp();
}
return UIProp.instance;
}
/*
单例模式,仅允许通过 UIProp.getInstance 获取全局唯一实例
*/
private constructor() {
console.log("创建函数");
}
public Start()
{
console.log("UI数据初始化");
}
public Update()
{
}
}
The calling method is as follows:
UIProp.getInstance().Update();
But it is obviously troublesome to write this way, especially when there are many singletons. And I thought that in C#, it can be solved by static + generic method. Since the grammatical features of ts are very similar to C#, there is no reason why it cannot be simplified, so I found the following way of writing:
//实例化脚本:基类
export class Singleton<T>{
private static inst: any = null;
public static Inst<T>(s: { new(): T }): T
{
if (this.inst == null)
{
this.inst = new s();
}
return this.inst;
}
}
When you want to create a singleton class later, you only need to write it like this.
import { Singleton } from "./Singleton";
/**
* name
*/
export class Test extends Singleton<Test>{
public Start()
{
console.log("初始化成功Test");
}
}
The calling method is as follows:
Test.Inst(Test).Start();
We simply tested and the results are as follows:
For the time being, we will update so much first, and the data storage and meter reading will be updated after the Dragon Boat Festival.
~~~~~~~2023/6/18~~~~~~~~