How Can I Get the Device Camera Aspect Ratio?

I’ve been using the WebCamTexture class to implement device camera views on mobile devices (both iPhones and Androids). However, I’ve noticed that the aspect ratio of the camera on a device does not necessarily match the aspect ratio of the screen (esp. on the wide-screen Androids). I can force the aspect to be 4:3 (which suits MOST cases), but what if the device’s camera uses something different?

I can’t find any information in WebCamTexture or WebCamDevice to help get the appropriate aspect ratio. Can anyone help?

This is quite belated, but I was prepping a test project to submit with a bug report, and discovered a solution.

Following what rutter suggested, you can indeed request a high resolution and framerate, and will generally get the best available (e.g ask for 1080p at 60fps and you’ll get 720p at 30fps). This is NOT ideal by any means, but it gets you something. The problem then is with the “16x16” size Unity seems to report for the camera texture, as mentioned by BTamas. This is what Unity gives back on an iPad even with the latest version (4.0.1) after requesting Play on the camera texture. With these (incorrect) numbers it seems impossible to set the dimensions correctly.

HOWEVER, the cause of this seems to be that starting the camera is asynchronous. By constantly checking them after starting the camera, I found that I would soon get correct values across all my devices (iPads 2, 3 and 4, iPhone 5 and Samsung Galaxy wifi 5.0 all worked). There doesn’t seem to be any way of knowing when these values will be correct, though, so you must monitor them in a script, rather than simply setting the size of your plane/surface/shader at one particular moment.

The solution in brief:

  1. Set the desired resolution to be very high, as rutter suggested.
  2. After asking the camera to start, monitor the size of the texture for a while (keep checking the width and height in the Update method of a script). It should be set to the correct size eventually. (Possibly several frames?)

Note: This solution will only allow you to get the highest available resolution. It would still be better to be able to read a set of supported resolutions ahead of time so you can choose a more appropriate one.

#Here’s the state of the art solution for 2016:

Hope it helps someone!

What I found is that the ratio will be correct after the device is set and you call Play() on the WebcamTexture.