2014年9月29日月曜日

UNITYで画面サイズをオブジェクト幅に合わせる

オセロや将棋盤を画面いっぱいに表示する場合のサンプルソース

・前提
メインカメラは上から盤面を見下ろす
例 positon(0,15,0) rotation(90,0,0)
盤の表面は座標 x0,y0を中心にする



using UnityEngine;
using System.Collections;

public class gamen : MonoBehaviour {

void Start () {
Camera cam = Camera.main;
float nFocalLength = focalLength (cam.fieldOfView, 10.0f);//盤の幅
cam.transform.position = new Vector3(transform.position.x, nFocalLength, transform.position.z);
}
float focalLength (float fov, float aperture) {
float nHalfTheFOV = fov / 2.0f * Mathf.Deg2Rad;
float nFocalLength = (0.5f / (Mathf.Tan (nHalfTheFOV) / aperture));
nFocalLength *= ((float)Screen.height / (float)Screen.width);
return nFocalLength;
}
}


0 件のコメント:

コメントを投稿