roguelike2d 摄像机参数设置

来源:互联网 发布:穿越火线抽奖软件 编辑:程序博客网 时间:2024/06/05 10:23

基于回合制的、基于瓦片地图的、roguelike的2d的scavenger游戏项目,其中摄像机的设置参数值在视频教程中并没有说明如何计算出来的。Hoxily尝试修改棋盘的rows和columns值的时候,游戏画面就不对了。

首先设置摄像机的目标是将棋盘恰好不漏包含在游戏画面中。

然后注意到资源中的sprite导入参数是32像素每单位长度,每个sprite恰好是1个单位的长与宽。

摄像机处于orthographics正投影时,Size参数控制的是摄像机的一半高度。而摄像机的宽度则由屏幕的宽高比和摄像机的高度两个因素确定。

sprite的transform.position是该sprite的正中位置。

因此计算摄像机参数过程如下:

using UnityEngine;using System.Collections;[RequireComponent(typeof(Camera))]public class CameraManager : MonoBehaviour{private Camera _camera;void Awake (){_camera = GetComponent<Camera> ();}/// <summary>/// 根据棋盘的大小,自动计算出相机的orthographicsSize,以及transform位置/// </summary>/// <param name="columns">Columns.</param>/// <param name="rows">Rows.</param>public void SetupCamera (int columns, int rows){float boardHeight = rows + 2;float boardWidth = columns + 2;float boardAspectRatio = boardWidth / boardHeight;float screenAspectRatio = (float)Screen.width / (float)Screen.height;if (boardAspectRatio <= screenAspectRatio) {// black strip at left and right// orthographicsSize * 2 == boardHeight_camera.orthographicSize = boardHeight / 2f;} else {// black strip at up and down// orthographicsSize * 2 * screenRation == boardWidth_camera.orthographicSize = boardWidth / screenAspectRatio / 2f;}float x = Center (-1f, -1f + boardWidth) - 0.5f;float y = Center (-1f, -1f + boardHeight) - 0.5f;float z = transform.position.z;transform.position = new Vector3 (x, y, z);}float Center (float min, float max){return (min + max) / 2f;}}


0 0
原创粉丝点击