分享3d场景下如何控制相机做屏幕适配

view.on("canvas-resize", () => this.setFov()) //监听游戏尺寸变化
setFov(){
    //获取 3d场景下的相机
    let camera = this.node?.getComponent(Camera);
    const visibleSize = view.getVisibleSize();
    if (visibleSize.width / visibleSize.height < view.getDesignResolutionSize().width / view.getDesignResolutionSize().height){ 
        camera.fovAxis = renderer.scene.CameraFOVAxis.HORIZONTAL;
        camera.fov = 37;//相机中设置 fovAxis 为 HORIZONTAL 后 调整合适的视角拿到的固定fov
    }else{
        camera.fovAxis = renderer.scene.CameraFOVAxis.VERTICAL;
        this.node.getComponent(Camera).fov = 20;
        camera.fov = 20;//相机中设置 fovAxis 为 VERTICAL 后 调整fov参数合适的视角拿到的固定数值

    }
}
1赞

只需要调整调整 横向竖向的fovAxis 时的fov 填入对应代码即可

(帖子被作者删除,如无标记将在 24 小时后自动删除)