Hi all,
I am adapting a game for iPhone to iPad. I have used Viewport relative coordinates (0.0 - 1.0) and size for 2D objects, but, of course, the aspect ratio is different and therefore 2D objects appears a bit deformed on iPad.
To fix this I am changing the Camera.rect property and leaving the less space possible not used by the camera. This works quite well besides the fact that the small areas of screen non used by the camera keep flashing with garbage - almost - random pixels.
Any idea to remove this undesired effect of the Camera.rect property adapting?
Here is the script I am using to adapt the viewport in order to keep original aspect ratio:
public class CameraScreenViewportResizer : MonoBehaviour
{
public float _origScreenWidth = 480.0f;
public float _origScreenHeight = 320.0f;
void Start()
{
float origScreenRatio = _origScreenWidth / _origScreenHeight;
float screenRatio = (float)Screen.width / (float)Screen.height;
Rect newViewportRect = new Rect();
float w = 0.0f;
float h = 0.0f;
if( screenRatio > origScreenRatio ){
w = (float)Screen.height * origScreenRatio;
newViewportRect.height = 1.0f;
newViewportRect.width = w / (float)Screen.width;
newViewportRect.y = 0.0f;
newViewportRect.x = (1.0f - newViewportRect.width) / 2.0f;
}
else{
h = (float)Screen.width / origScreenRatio;
newViewportRect.width = 1.0f;
newViewportRect.height = h / (float)Screen.height;
newViewportRect.x = 0.0f;
newViewportRect.y = (1.0f - newViewportRect.height) / 2.0f;
}
for(int i=0; i < Camera.allCameras.Length; i++){
Camera cm = Camera.allCameras[i];
cm.rect = newViewportRect;
}
}
}
Many thanks.