unity 获取水平FOV
unity中Camera的Field of View是指的垂直FOV,水平FOV可以经过计算得到。
创建脚本如下,把脚本挂载到摄像机上即可得到水平FOV:
public class GetHorizontalFov : MonoBehaviour {
Camera myCamera;
float distance = 10 - 0.015f;
void Start () {
myCamera = gameObject.GetComponent<Camera>();
float cameraHeight = 2.0f * distance * Mathf.Tan(myCamera.fieldOfView * 0.5f * Mathf.Deg2Rad);//视锥体高度
var cameraWidth = cameraHeight * myCamera.aspect;//视锥体宽度
var horizontalfov = 2 * Mathf.Atan(cameraWidth * 0.5f / distance) * Mathf.Rad2Deg;//水平FOV
Debug.LogError("horizontalfov: " + horizontalfov);
}
}