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:
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.
answered Feb 08 at 11:41 PM
Jason RT Bond
I was going to say that it looks like you could just divide the WebCamTexture's width and height to estimate aspect ratio, but it looks like you have to set a requested resolution before getting any information back? That's tricky.
This seems hacky, but maybe you could request a texture with insanely high resolution (in an attempt to get the camera's actual max resolution), and then use that texture to pull out aspect information.
answered Mar 26 '12 at 06:39 PM