1.屏幕的寬高比Aspect Ratio = 屏幕寬度/屏幕高度
2.當(dāng)攝像機(jī)orthographicSize屬性值等于當(dāng)前屏幕高度單位的一半時(shí),攝像機(jī)大小正好與屏幕大小(移動(dòng)端的實(shí)際屏幕大小)相等。
【注】這里的高度單位是經(jīng)過(guò)像素到單位比即Pixels To Units換算的,Unity2D中這個(gè)比例的默認(rèn)值是100,即100像素等于1單位。假設(shè)我們的游戲屏幕有640像素高,那么實(shí)際換算成單位高度則是6.4個(gè)單位,當(dāng)我們攝像機(jī)的orthographicSize值是3.2時(shí),攝像機(jī)大小剛好與屏幕大小相等。
攝像機(jī)實(shí)際寬度 = 攝像機(jī)orthographicSize * 2 * 屏幕寬高比
等同于
攝像機(jī)實(shí)際寬度 = 攝像機(jī)高度 * 屏幕寬高比
問(wèn)題展示:
當(dāng)我們?cè)谑褂胕Phone4(640x960)尺寸的時(shí)候,此時(shí)屏幕的單位高度是9.6,orthographicSize為4.8,攝像機(jī)大小正好與屏幕大小(移動(dòng)端的實(shí)際屏幕大小)相等,并且單位寬度是6.4.
但是我們此界面展示在iPhone5(640x1136)尺寸的時(shí)候,效果卻不盡人意,如圖:
我們看到場(chǎng)景中的游戲?qū)ο髮⒔幸话氡粩z像機(jī)裁剪掉了。
原因如下:
Aspect Ratio = 640/1136 = 9/16;
攝像機(jī)的高度 = 4.8 * 2 = 9.6;
攝像機(jī)的寬度 = 9.6 * 9 / 16= 5.4
5.4小于6.4,所以我們就看到了游戲?qū)ο蟊徊眉袅艘徊糠郑绻覀円^續(xù)保持和iPhone4上的寬度,那么我們可以調(diào)整orthographicSize
攝像機(jī)高度 = 攝像機(jī)寬度/Aspect Ratio = 攝像機(jī)寬度 * 16/9=6.4*16/9=11.38;
orthographicSize = 攝像機(jī)高度/2 = 5.69;
效果圖如下:
usingUnityEngine;
usingSystem.Collections;
publicclassTest : MonoBehaviour {
//基于iPhone4比例的單位高度
float ?devHeight =9.6f;
//基于iPhone4比例的單位寬度
float devWidth =6.4f; ?// Use this for initialization
void Start()
{?
float ?screenHeight = Screen.height;
Debug.Log("screenHeight = "+ screenHeight);//攝像機(jī)的尺寸floatorthographicSize =this.GetComponent().orthographicSize;//寬高比f(wàn)loataspectRatio = Screen.width *1.0f / Screen.height;//攝像機(jī)的單位寬度f(wàn)loatcameraWidth = orthographicSize *2* aspectRatio;
Debug.Log("cameraWidth = "+ cameraWidth);//如果設(shè)備的寬度大于攝像機(jī)的寬度的時(shí)候? 調(diào)整攝像機(jī)的orthographicSizeif(cameraWidth < devWidth)
{
orthographicSize = devWidth / (2* aspectRatio);
Debug.Log("new orthographicSize = "+ orthographicSize);this.GetComponent().orthographicSize = orthographicSize;
}
}
}