Layaair 3D场景使用

环境: Layaair  1.7.22

            Unity3d 5.6.3

实现效果:

  1. unity3d 导出 场景文件
  2. 动画的控制
  3. Laya.Sprite3D 脚本的挂载
  4. 对于Laya.Sprite3D 碰撞的触发实现

 

Layaair 3D场景使用_第1张图片

 

Unity3D 中的场景

Layaair 3D场景使用_第2张图片

 

程序入口

 // 程序入口
class GameMain {
    constructor() {
        //初始化引擎
        Laya3D.init(0, 0, true);

        //适配模式
        Laya.stage.scaleMode = Laya.Stage.SCALE_FULL;
        Laya.stage.screenMode = Laya.Stage.SCREEN_NONE;

        //开启统计信息
        Laya.Stat.show();

        //添加3D场景
        //var scene: Laya.Scene = Laya.stage.addChild(new Laya.Scene()) as Laya.Scene;

        // //添加照相机
        // var camera: Laya.Camera = (scene.addChild(new Laya.Camera(0, 0.1, 100))) as Laya.Camera;
        // camera.transform.translate(new Laya.Vector3(0, 3, 3));
        // camera.transform.rotate(new Laya.Vector3(-30, 0, 0), true, false);
        // camera.clearColor = null;

        //添加方向光
        // var directionLight: Laya.DirectionLight = scene.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        // directionLight.color = new Laya.Vector3(0.6, 0.6, 0.6);
        // directionLight.direction = new Laya.Vector3(1, -1, 0);

        // //添加自定义模型
        // var box: Laya.MeshSprite3D = scene.addChild(new Laya.MeshSprite3D(new Laya.BoxMesh(1, 1, 1))) as Laya.MeshSprite3D;
        // box.transform.rotate(new Laya.Vector3(0, 45, 0), false, false);
        // var material: Laya.StandardMaterial = new Laya.StandardMaterial();
        // material.diffuseTexture = Laya.Texture2D.load("res/layabox.png");
        // box.meshRender.material = material;

        ///需要加载的3D资源列表  和 UI资源  
        let resArray : Array = [
            { url:"res/Rources3D/LayaScene_Shooting/Shooting.ls",   type:SceneBehavior, clas:SceneBehavior}, //3D 场景
            { url:"res/atlas/comp.atlas",                           type:Laya.Loader.ATLAS}, 
        ];            
        Laya.loader.create(resArray, Laya.Handler.create(this,this.onCompelt),Laya.Handler.create(this, this.onProgress));
    }

 
            //加载完成
    private onCompelt():void   
    {
         let scene:SceneBehavior   =  Laya.loader.getRes("res/Rources3D/LayaScene_Shooting/Shooting.ls");
         Laya.stage.addChild(scene);    

        //  var directionLight: Laya.DirectionLight = scene.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        //  directionLight.color = new Laya.Vector3(0.6, 0.6, 0.6);
        //  directionLight.direction = new Laya.Vector3(1, -1, 0);  

         let camera = scene.Camar;
         camera.addComponent(CamerBehavior);

         let ball :Laya.MeshSprite3D = scene.Ball;
         ball.addComponent(BallBehavior);

         let wall : Laya.Sprite3D = scene.Wall;
         wall.addComponent(WallBehavior);

         let cube : Laya.Sprite3D = scene.Cube;
         cube.addComponent(CubeBehavior);

         let rigid:Laya.Sprite3D = scene.BallRigid;
         rigid.addComponent(RigidbodyBehavior);
         

        //  console.log("position.name:",ball.name);
        //  console.log("position:",ball.transform.position);

         let uiMain:UiGameMain =new UiGameMain(cube.getComponentByType(CubeBehavior));
         Laya.stage.addChild(uiMain);

    }
     //加载进度
    private onProgress(pro:number):void
    {
        console.log("加载进度:"+pro);
    }
}
new GameMain();

Layaair 3D场景使用_第3张图片

 clas:SceneBehavior  是我们自定义的场景 类型

Layaair 3D场景使用_第4张图片

加载完成之后 就直接把我们的资源 和 SceneBehavior 绑定了

 /*
* Scene 场景管理;
*/
class SceneBehavior extends Laya.Scene {

    public get Ball():Laya.MeshSprite3D 
    {
        return this.getChildAt(3) as Laya.MeshSprite3D;
    }

    public get Wall():Laya.MeshSprite3D 
    {
        return  this.getChildAt(2) as Laya.MeshSprite3D;
    }

    public get Camar():Laya.MeshSprite3D 
    {
        return this.getChildAt(0) as Laya.MeshSprite3D;
    }

    public get Cube():Laya.MeshSprite3D 
    {
        return this.getChildAt(4) as Laya.MeshSprite3D;
    }

    public get BallRigid():Laya.MeshSprite3D 
    {
        return this.getChildAt(5) as Laya.MeshSprite3D;
    }

    constructor(){
        super();
        console.log("SceneBehavior");
    }
 
}

场景中是有 动画 播放的 处理脚本在 这里

 /*
* name;
*/
class UiGameMain extends ui.MainSceneUI{
    private cube:CubeBehavior; 
    private ani:Laya.Animator;
    constructor(cube:laya.d3.component.Component3D){
        super();

        this.on(Laya.Event.CLICK, this, this.onClick);       

        this.cube = cube as CubeBehavior;
        this.ani  = this.cube.gameobject.getComponentByType(Laya.Animator) as Laya.Animator;

          
    }

    private onClick(e:Laya.Event):void
    {
        switch(e.target)
        {
            case this.btn_Play:
                console.log("btn_Play");
                //this.ani.currentPlayClip;
                this.ani.play("move");
                this
            break;
            case this.btn_stop:
                 console.log("btn_stop");
                 this.ani.play("idle");
            break;

            case this.btn_add:
            
            break;
        }      
    }


}

 碰撞触发器在这里

  • /*
    * name;
    */
    class CubeBehavior extends Laya.Script{
        public gameobject:Laya.Sprite3D;
        constructor(){
            super()
        }
    
        public _initialize(owner: Laya.Sprite3D): void {
            super._initialize(owner);
            this.gameobject = owner;
    
        }
    
    
        ///碰撞器和 触发器的区别是 一个可以穿透 一个不可以
    
        //触发器 开始 可以触发
        public onTriggerEnter(other: Laya.Collider): void
        {
            console.log("cube==> onTriggerEnter");
        }
     
        public onTriggerExit(other: Laya.Collider): void
        {
            console.log("cube==> onTriggerExit");
        }
    
        public onTriggerStay(other: Laya.Collider): void
        {
           // console.log("cube==> onTriggerStay");
        }
        //触发器 完
    
    
        //碰撞器 不可以触发
        public  onCollisionEnter(collision:Laya.Collision):void
        {
            console.log("cube==> onCollisionEnter");
        }
    
        public  onCollisionStay(collision:Laya.Collision):void
        {
    
        }
    
        public  onCollisionExit(collision:Laya.Collision):void
        {
            console.log("cube==> onCollisionExit");
        }
        //碰撞器
    }

    这里上传了 U3D 和 Layaair 的工程

你可能感兴趣的:(layaair)