- Home /
Changing camera aspect ratio with non-fullscreen viewport
Hey,
In my Unity application I have a screen which contains a right hand sidebar and a footer as GUI components. In the upper left of the screen I have my main camera with it's viewport rect adjusted to fit that area. This viewport contains a scene in which the user creates content, hence the sidebar and footer which contain controls (such as place asset). I hope that makes sense.
The content is to be displayed on devices with varying aspect ratios so I allow the user to switch aspect ratio in order to preview what the content would look like on the given device. I control the aspect ratio with a behavior that I derived from: http://wiki.unity3d.com/index.php?title=AspectRatioEnforcer
When the script is used full screen it works fine. By fine I mean that whatever the width and height the screen (free-aspect view) the content appears correctly - as in it is letterboxed/pillarboxed and the exact same area of content is displayed in all cases.
In order to restrict the viewport area in my user interface to the upper left I attempted to add padding variables. The result does render the viewport with the correct position and size but for some reason at certain dimensions the content displayed in the viewport is shifted and this is not acceptable for my use case.
So basically my padding modifications have altered the actual content that appears in the viewport rather than simply resizing the container. What I intended to occur was for the letterboxing/pillarboxing to accommodate the padding and display as per when full screen.
Here is my script:
public class CameraAspectRatioController : MonoBehaviour
{
// Unity visible properties
public Camera Camera;
public float PaddingTop;
public float PaddingLeft;
public float PaddingBottom;
public float PaddingRight;
//
private float[] aspectRatio = new[]
{
// 16:9 - iPhone 5, 6, 6+
(16.0f/9.0f),
// 3:2 - iPhone
(3.0f/2.0f),
// 4:3 - iPad
(4.0f/3.0f)
};
private int aspectRatioIndex = 0;
private void Awake()
{
if (Camera == null)
Camera = UnityEngine.Camera.main;
RenderSizeChangeListener.OnRenderSizeChanged += () => ShiftAspectRatio();
}
private void Start()
{
ShiftAspectRatio();
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.UpArrow))
ShiftAspectRatio(-1);
if (Input.GetKeyUp(KeyCode.DownArrow))
ShiftAspectRatio(1);
}
private void ShiftAspectRatio(int diff = 0)
{
this.aspectRatioIndex += diff;
if (this.aspectRatioIndex < 0)
this.aspectRatioIndex = this.aspectRatio.Length - 1;
else if (this.aspectRatioIndex > this.aspectRatio.Length - 1)
this.aspectRatioIndex = 0;
var targetAspect = aspectRatio[this.aspectRatioIndex];
SetAspectRatio(targetAspect);
}
private void SetAspectRatio(float targetAspect)
{
var currentAspect = Width / Height;
// If the current aspect ratio is already approximately equal to the desired aspect ratio,
// use a full-screen Rect (with padding) (in case it was set to something else previously)
if (Mathf.Approximately(currentAspect, targetAspect))
{
Camera.rect = new Rect(UnitLeft, UnitBottom, UnitWidth, UnitHeight);
return;
}
// Pillarbox
if (currentAspect > targetAspect)
{
var inset = 1.0f - targetAspect/currentAspect;
Camera.rect = new Rect(UnitLeft + (inset/2), UnitBottom, UnitWidth-inset, UnitHeight);
}
// Letterbox
else
{
var inset = 1.0f - currentAspect/targetAspect;
Camera.rect = new Rect(UnitLeft, UnitBottom + (inset/2), UnitWidth, UnitHeight-inset);
}
}
private float Width
{
get { return Screen.width - PaddingLeft - PaddingRight; }
}
private float Height
{
get { return Screen.height - PaddingTop - PaddingBottom; }
}
private float UnitWidth
{
get { return Width / Screen.width; }
}
private float UnitHeight
{
get { return Height / Screen.height; }
}
private float UnitLeft
{
get { return PaddingLeft / Screen.width; }
}
private float UnitRight
{
get { return (Screen.width - PaddingRight) / Screen.width; }
}
private float UnitTop
{
get { return (Screen.height - PaddingTop) / Screen.height; }
}
private float UnitBottom
{
get { return PaddingBottom / Screen.height; }
}
}
If anyone has any ideas that would be gratefully appreciated. I hope my question makes sense.
Thanks in advance...
Cheers, Mark