C# 如何修改WebCamTexture的大小?

C# 如何修改WebCamTexture的大小?,c#,unity3d,textures,webcam,C#,Unity3d,Textures,Webcam,我从Unity中的webcamtexture获得纹理,但对于我的项目来说,它太高了。 我不需要它1280x720,但我会减少它的大小。 如何在Unity中更改it参数? 非常感谢。谢谢你的建议 您可以使用所请求的高度和宽度来调整想要从相机获得的纹理大小。但是,您需要在相机未运行时执行此操作,并且您将获得设备本身支持的最接近的大小 webCamTexture.requestedWidth和webCamTexture.requestedHeight返回最初尝试初始化webCamTexture时使用的

我从Unity中的webcamtexture获得纹理,但对于我的项目来说,它太高了。 我不需要它1280x720,但我会减少它的大小。 如何在Unity中更改it参数?
非常感谢。谢谢你的建议

您可以使用所请求的高度和宽度来调整想要从相机获得的纹理大小。但是,您需要在相机未运行时执行此操作,并且您将获得设备本身支持的最接近的大小


webCamTexture.requestedWidth和webCamTexture.requestedHeight返回最初尝试初始化webCamTexture时使用的宽度/高度。它们不能反映真实的宽度/高度。webCamTexture.width/webCamTexture.height也不会一直显示。问题在于,在某些情况下,由于设备摄像头的硬件和分辨率不同,webCamTexture的大小会在一段时间后确定,这通常与设备的屏幕不同

您可以做的是获取像素webCamTexture.GetPixels 1次,然后在一帧之后,您可以使用webCamTexture.width/.height返回捕获的纹理大小

插件CameraCaptureKit中的此类代码示例,该插件具有执行您所描述的功能

// ANDREAS added this: In some cases we apparently don't get correct width and height until we have tried to read pixels
    // from the buffer.
    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代码时,你应该能够通过将requestedWidth/requstedHeight设置为较小的值来设置WebCamTexture的较小纹理大小,Unity将至少在iOS上尝试并选择最接近你所需纹理大小的相机分辨率

这发生在CameraCapture.mm中

- (NSString*)pickPresetFromWidth:(int)w height:(int)h
{
static NSString* preset[] =
{
    AVCaptureSessionPreset352x288,
    AVCaptureSessionPreset640x480,
    AVCaptureSessionPreset1280x720,
    AVCaptureSessionPreset1920x1080,
};
static int presetW[] = { 352, 640, 1280, 1920 };

//[AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];


#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
 }

谢谢@Dover8,我试图在代码中使用这些属性,但没有结果!在WebCamTexture实例化之后,我将Width和height设置为512,但在运行时没有设置。你能给我举个小例子吗?谢谢。换句话说,如果您点击预设尺寸352,Unity将选择较低分辨率的WebCamTexture。。。。640... 等等