我正在寻找如何使相机适合游戏对象,我找到了这个答案:
在Unity/c#中改变相机的大小以适应GameObject;我不明白这部分
cam.orthographicSize = ((w > h * cam.aspect) ? (float)w / (float)cam.pixelWidth * cam.pixelHeight : h) / 2;
我想了解这部分代码是如何工作的。
如果你不理解condition ? A : B
也许这对你有帮助
if(w > h * cam.aspect){
cam.orthographicSize = ((float)w / (float)cam.pixelWidth * cam.pixelHeight ) / 2
}
else{
cam.orthographicSize = h / 2
}
这只是把它一块一块地分解的问题。让我们看看:
cam.orthographicSize =
STEP 1: ((w > h * cam.aspect) ? (float)w / (float)cam.pixelWidth * cam.pixelHeight : h)
STEP 2: [STEP 1] / 2
步骤1分为以下几个部分:
STEP 1.1: h * cam.aspect (let's call this "JOHN")
STEP 1.2: (float)cam.pixelWidth * cam.pixelHeight (let's call this "WENDY")
STEP 1.3:
三元操作符(这只是一个IF else以一种奇特的方式写成)
IF w is greater than "JOHN" THEN
RETURN (float)w / "WENDY"
ELSE
RETURN h
最后,无论从步骤1得到什么,都被分成2(步骤2)。