Unity WebCamTexture looks squashed in RawImage
I am currently trying to get a WebCamTexture to format correctly so that it has the correct aspect ratio on all devices. Right now, although, I am getting a vertically squashed image.
I have the live camera feed set up so that it sits in a RawImage (that exists in a Canvas) like the following:
public class CameraController : MonoBehaviour {
private WebCamTexture _frontCam;
private bool _camAvaliable;
public RawImage background;
public AspectRatioFitter fit;
private void Start() {
var devices = WebCamTexture.devices;
foreach (var device in devices) {
// Found front camera and created running texture
_camAvaliable = ApplyInitialTexture(device);
if (_camAvaliable) break;
}
}
private bool ApplyInitialTexture(WebCamDevice device) {
if (!device.isFrontFacing) return false;
_frontCam = new WebCamTexture(device.name, (int)(Screen.height/2.5), (int)(Screen.width/2.5));
_frontCam.Play();
background.texture = _frontCam;
return true;
}
private void Update()
{
if (!_camAvaliable) return;
var ratio = _frontCam.width / _frontCam.height;
fit.aspectRatio = ratio; // Set the aspect ratio
var scaleY = _frontCam.videoVerticallyMirrored ? 1f : -1f; // Find if the camera is mirrored or not
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
// Swap the mirrored camera
var orient = -_frontCam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
}
The CameraController is tied to a UIManager that is fed the RawImage and the AspectRatioFitter from the Scene.
The AspectRatioFitter on the RawImage is set up like the following:
And the result after all this is a vertically squashed image. Is something set up incorrectly? Ideally I would like the image to have a normal aspect ratio like Snapchat or just the native camera app.
Here's an example:
Your answer
Follow this Question
Related Questions
How to load a folder of textures automatically to an array of Raw Images? 2 Answers
How can I reset the score counter? 0 Answers
RawImage.texture to Texture2D 3 Answers
WebCamTextures square 0 Answers
Texture file is all alpha when resizing 0 Answers