Laya 상용 등급 3D 실제 전투 -08 원활한지도 및 개체 풀의 첫 번째 버전


개체의 원활한 생성 / 재활용 달성하기위한 이 섹션 의 목표

laya 상업 3d 게임 개발

Laya로 장면 재질 내보내기
여기에 사진 설명 삽입

장면 만들기

新建 Example04_Spawn.ts
내보내기 클래스 Example04_Spawn 확장 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;
}

시작시 변경

여기에 사진 설명 삽입
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
는 개체 풀을 사용하여 메모리 및 스프라이트 수치 안정성을 테스트합니다.
여기에 사진 설명 삽입
여기에 사진 설명 삽입

추천

출처blog.csdn.net/koljy111/article/details/108020005