Laya商业级3d实战-08无缝地图初版与对象池

本节目标
实现无缝创建/回收 对象

laya商业级3d游戏开发

导出场景素材到Laya
在这里插入图片描述

进行场景的构建

新建Example04_Spawn.ts
export class Example04_Spawn extends Laya.Script {
scene:Laya.Scene3D;
}

Mian.ts
onConfigLoaded(): void {
this.example_spwan();

}


 example_spwan() {
    let node = new Laya.Node();
    Laya.stage.addChild(node);
    node.addComponent(UnityEnagine)
    SceneManager.LoadSceneByName('Example_BuildLoop', this, (p_Scene3D) => {

        Laya.stage.addChild(p_Scene3D);
        let examlpe05_Spawn = new Example04_Spawn()
        examlpe05_Spawn.scene = p_Scene3D;
        node.addComponentIntance(examlpe05_Spawn);
    });
}

实现Example04_Spawn

需求主干整理
无缝地图伪代码
Update()
{
//定义线段的 开始 和结束 范围 currentz ,endz

//在范围内创建物体
//回超出范围物体

}

实现: 创建物体
在这里插入图片描述

ExamlpeSpawn.ts
onStart() {
this.CloneGo();
}

protected CloneGo(): Laya.Sprite3D {
    //原型
    // var gob = this.scene.getChildByName('Resources').getChildByName('BuildItem').getChildByName('IndustrialWarehouse01') as Laya.Sprite3D
    //框架封装,查找路径对象
    var gob = GameObject.Find<Laya.Sprite3D>(this.scene, 'Resources/BuildItem/IndustrialWarehouse01');
    var newGo = Laya.Sprite3D.instantiate(gob);
    this.scene.addChildren(newGo);
    newGo.active = true;
    return newGo;
}

F8 F5,此时场景实例化了创建了指定的对象

在这里插入图片描述

//实现:在指定的范围内使用对象池创建物体
//设开始坐标为0,长度为100
currentZ = 0;
startCreateZ = 0;
length = 100;

protected endZ(): number {
    return this.currentZ + this.length;
}

//区间内创建物体
Create2End() {
    var p_endZ = this.endZ();
    while (this.startCreateZ < p_endZ) {

        let length = this.CreateItem(this.startCreateZ);
        this.startCreateZ += length;
    }
}

//创建物体设置坐标,返回物体长度
protected CreateItem(z: number): number {
    //对象池创建Laya.Pool.getItemByCreateFun

    var newGo = Laya.Pool.getItemByCreateFun('IndustrialWarehouse01', () => { return this.CloneGo(); }, this) as Laya.Sprite3D;
    newGo.active = true;
    this.scene.addChild(newGo);
    newGo.transform.position = new Laya.Vector3(0, 0, z);;
    return 18;
}

更改onstart

在这里插入图片描述
F5
在这里插入图片描述

实现:超出范围后回收

增加字段
runtimeGobs: Laya.Sprite3D[] = [];

补上对象实例表,进行回收管理
CloneGo() …
//给回收用的
this.runtimeGobs.push(newGo as Laya.Sprite3D);

 //回收超出范围的且在场景内的物体
recoverLessZ() {
    for (const item of this.runtimeGobs) {
        //在场景内的物体?
        if (item.displayedInStage) {
            let height = 18;
            if (item.transform.position.z + height * 0.5 < this.currentZ) {
                Laya.Pool.recover(item.name, item);
                item.removeSelf();
            }
        }
    }
}

onUpdate() {
this.currentZ += 1;
//在范围内 创建物体
this.Create2End();
//回收超出范围的物体
this.recoverLessZ();
}

F5
因为使用了对象池 测试内存和sprite数值 稳定
在这里插入图片描述
在这里插入图片描述

LayaAir 中,可以通过设置 `Socket` 对象的 `protocols` 属性来添加 `Sec-WebSocket-Protocol` 头部。例如: ```javascript const socket = new Socket(); socket.protocols = ['protocol1', 'protocol2']; ``` 其中,`protocols` 是一个数组,包含了要使用的协议名称。在连接到 WebSocket 服务器时,LayaAir 会在握手请求中添加 `Sec-WebSocket-Protocol` 头部,以告知服务器使用的协议。 以下是完整代码示例: ```javascript import Socket = Laya.Socket; const socket = new Socket(); socket.protocols = ['protocol1', 'protocol2']; socket.on(Laya.Event.OPEN, this, onSocketOpen); socket.on(Laya.Event.MESSAGE, this, onSocketMessage); socket.on(Laya.Event.CLOSE, this, onSocketClose); socket.on(Laya.Event.ERROR, this, onSocketError); socket.connect('ws://localhost:8080', 8080); function onSocketOpen() { console.log('WebSocket connected'); } function onSocketMessage(data) { console.log('Received message:', data); } function onSocketClose() { console.log('WebSocket closed'); } function onSocketError(e) { console.error('WebSocket error:', e); } // 发送消息 socket.send('Hello, WebSocket!'); // 关闭连接 socket.close(); ``` 在上述示例中,我们通过设置 `socket.protocols` 属性来添加 `Sec-WebSocket-Protocol` 头部。在服务端,需要检查客户端发送过来的 `Sec-WebSocket-Protocol` 头部,以确定客户端请求使用的协议是否被支持。若支持,则服务端需要在握手响应中添加 `Sec-WebSocket-Protocol` 头部,以告知客户端使用的协议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程之力

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

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

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

打赏作者

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

抵扣说明:

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

余额充值