在使用 Unity 製作 2D 遊戲時,如果要維持攝影機拍攝的長寬比例不變,但是卻又要動態地調整拍攝的範圍,那麼我們可以選擇在程式中依照需求調整 orthographicSize
這個參數。
這個參數,會直接影響到攝影機屬性中的 size
。
![](https://i0.wp.com/clay-atlas.com/wp-content/uploads/2021/06/image-23.png?resize=660%2C818&ssl=1)
調整攝影機 size 的程式碼
最快速的做法,就是使用:
Camera.main.orthographicSize = SIZE;
SIZE
就是你希望的攝影機範圍尺寸。
References
- https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html
- https://answers.unity.com/questions/174002/what-is-the-relationship-between-camera-size-units.html