舉個例子,在 iOS 640x1136 底下,我們看到的是這個樣子
![]() |
9:16 |
![]() |
2:3 |
在這邊提供一個小技巧。
我們只要先得知固定寬度,再倒著算回去 Camera 在該比率下應該有的高度即可。
程式範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent(typeof(Camera))] | |
public class TodoCameraFixedWidth : MonoBehaviour { | |
#region Variables | |
// 想要固定的螢幕寬度 | |
public float m_width = 640.0f; | |
// 被指定的 Camera | |
private Camera m_camera; | |
#endregion | |
#region Behaviour | |
private void Awake() { | |
m_camera = camera; | |
} | |
private void Start() { | |
if( m_camera == null ) | |
return ; | |
// 倒著算回去 | |
float size = (float)m_width * Screen.height / Screen.width / 2; | |
// 重新指定新的螢幕高度 | |
m_camera.orthographicSize = size; | |
} | |
#endregion | |
} |
因為 NGUI 預設 Camera 的 OrthographicSize 是 1,所以我們可以得知在 9:16 的比率下寬度為
2 x 9 / 16 = 1.125
然後把該 Component 放入 Camera 物件中,指定寬度為 1.125
當遊戲執行後,會自動計算調整 Camera 的高度,結果如下圖
Camera 的寬度完美地適應了背景圖片!
有jave教學嗎?
回覆刪除