webCamTexture.requestedWidth and webCamTexture.requestedHeight returns the width / height with which you originally tried to initialize WebCamTexture. they do not reflect the actual width / height. Also, webCamTexture.width / webCamTexture.height always. The problem is that the size of webCamTexture is determined after some time in some cases due to poor hardware and camera resolution of the device, which is not the same aspect as the screen of the device.
What you can do is get the pixels (webCamTexture.GetPixels ()) 1 time, and then after the frame you can use webCamTexture.width / .height to return the captured texture size.
CameraCaptureKit (https://www.assetstore.unity3d.com/en/#!/content/56673), , , .
void TryDummySnapshot( ) {
if(!gotAspect) {
if( webCamTexture.width>16 ) {
if( Application.platform == RuntimePlatform.IPhonePlayer ) {
if(verbose)Debug.Log("Already got width height of WebCamTexture.");
} else {
if(verbose)Debug.Log("Already got width height of WebCamTexture. - taking a snapshot no matter what.");
var tmpImg = new Texture2D( webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false );
Color32[] c = webCamTexture.GetPixels32();
tmpImg.SetPixels32(c);
tmpImg.Apply();
Texture2D.Destroy(tmpImg);
}
gotAspect = true;
} else {
if(verbose)Debug.Log ("Taking dummy snapshot");
var tmpImg = new Texture2D( webCamTexture.width, webCamTexture.height, TextureFormat.RGB24, false );
Color32[] c = webCamTexture.GetPixels32();
tmpImg.SetPixels32(c);
tmpImg.Apply();
Texture2D.Destroy(tmpImg);
}
}
}
, iOS, WebCamTexture, "requestWidth/requstedHeight" - , Unity ( , iOS ) , requsted texturesize.
CameraCapture.mm
- (NSString*)pickPresetFromWidth:(int)w height:(int)h
{
static NSString* preset[] =
{
AVCaptureSessionPreset352x288,
AVCaptureSessionPreset640x480,
AVCaptureSessionPreset1280x720,
AVCaptureSessionPreset1920x1080,
};
static int presetW[] = { 352, 640, 1280, 1920 };
#define countof(arr) sizeof(arr)/sizeof(arr[0])
static_assert(countof(presetW) == countof(preset), "preset and preset width arrrays have different elem count");
int ret = -1, curW = -10000;
for(int i = 0, n = countof(presetW) ; i < n ; ++i)
{
if( ::abs(w - presetW[i]) < ::abs(w - curW) && [self.captureSession canSetSessionPreset:preset[i]] )
{
ret = i;
curW = presetW[i];
}
}
NSAssert(ret != -1, @"Cannot pick capture preset");
return ret != -1 ? preset[ret] : AVCaptureSessionPresetHigh;
#undef countof
}