参考文档:雨松MOMO的Unity3D研究院之获取摄像机的视口区域
Camera.fieldOfView
在学习之前,我先去查询了fieldOfView实际代表的意义,官方文档上的描述如下:
This is the vertical field of view; horizontal FOV varies depending on the viewport's aspect ratio.
也就是说,fieldOfView实际上是摄像机所在的位置的垂直面上的角度。那么,既然确定了含义,我们就可以根据三角函数来计算可视区域了。
不过,在此之前,我还想测试一下,处于手机端横竖屏的两种情况下,fov是否是真正意义上的垂直面角度。
测试代码:
FOV = 40;
void Start() {
Debug.LogError("Screen.width = " + Screen.width);
Debug.LogError("Screen.height = " + Screen.height);
Camera theCamera = Camera.main;
Debug.LogError("Camera.main.aspect = " + Camera.main.aspect);
float halfFOV = (theCamera.fieldOfView * 0.5f) * Mathf.Deg2Rad;
Debug.LogError("halfFOV = " + halfFOV);
float aspect = theCamera.aspect;
float height = 10 * Mathf.Tan(halfFOV);
Debug.LogError("height = " + height);
float width = height * aspect;
Debug.LogError("width = " + width);
Debug.LogError("-------------------------------------------");
}
测试1:正常竖屏,小米MIX2S,屏幕分辨率 2160:1080
Unity: Screen.width = 1080
Unity: Screen.height = 2160
Unity: Camera.main.aspect = 0.5
Unity: halfFOV = 0.3490658
Unity: height = 3.639702
Unity: width = 1.819851
测试2:倒置竖屏,小米MIX2S,屏幕分辨率 2160:1080
Unity: Screen.width = 1080
Unity: Screen.height = 2160
Unity: Camera.main.aspect = 0.5
Unity: halfFOV = 0.3490658
Unity: height = 3.639702
Unity: width = 1.819851
测试3:LandscapeLeft横屏,小米MIX2S,屏幕分辨率 2160:1080
Unity: Screen.width = 2160
Unity: Screen.height = 1080
Unity: Camera.main.aspect = 2
Unity: halfFOV = 0.3490658
Unity: height = 3.639702
Unity: width = 7.279405
测试3:LandscapeRight横屏,小米MIX2S,屏幕分辨率 2160:1080
Unity: Screen.width = 2160
Unity: Screen.height = 1080
Unity: Camera.main.aspect = 2
Unity: halfFOV = 0.3490658
Unity: height = 3.639702
Unity: width = 7.279405
OK,测试完毕,这次我可告诉自己,Unity的相机fov的垂直意义和我们习惯上使用手机时认为的垂直意义是一致的,并且宽度的可视范围也确实是根据 Camera.aspect 来计算的。
Camera.targetTexture
在unity群里装逼的时候,被打脸了,学习到了一个东西。使用targetTexture的时候,摄像机的视口设定是根据赋值的RenderTexture的比例来的,跟实际窗口无关。这个事实还蛮合理的。
其次,Canvas的ScreenSpcace模式下,关联了Camera,自身会出现无法修改的缩放值,这个缩放值,其实是来源于相机自身的可视范围,也就是世界坐标的视口大小与Canvas预设定的sizeDelta的比例。
另外,Canvas的RenderMode在不同的模式下,是非常非常不一样的,以往在UGUI上,总是能用的设置就可以了,一般都会忽略理解其中的含义,不过既然知道了这个知识点,以后遇到了机会,可以好好的冷静思考相关的来龙去脉了。