我正在设置统一的webcamtexture的焦距,但是我无法修改相机的焦距...
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MobileCam : MonoBehaviour {
private bool camAvailable;
private WebCamTexture cameraTexture;
//private Texture defaultBackground;
public RawImage background;
public AspectRatioFitter fit;
public bool frontFacing;
// Use this for initialization
void Start () {
//defaultBackground = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
return;
for (int i = 0; i < devices.Length; i++)
{
var curr = devices[i];
if (curr.isFrontFacing == frontFacing)
{
//1280*720
cameraTexture = new WebCamTexture(curr.name, Screen.currentResolution.width, Screen.currentResolution.height);
break;
}
}
if (cameraTexture == null)
return;
cameraTexture.Play (); // Start the camera
background.texture = cameraTexture; // Set the texture
camAvailable = true; // Set the camAvailable for future purposes.
}
// Update is called once per frame
void Update () {
if (!camAvailable)
return;
//float ratio = (float)cameraTexture.width / (float)cameraTexture.height;//3/2=1.3333 Vertical=2/3=0.666
//fit.aspectRatio = ratio; // Set the aspect ratio
float ratio = (float)cameraTexture.height / (float)cameraTexture.width ;//3/2=1.3333 Vertical=2/3=0.666
fit.aspectRatio = ratio; // Set the aspect ratio
float scaleY = cameraTexture.videoVerticallyMirrored ? -1f : 1f; // Find if the camera is mirrored or not
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f); // Swap the mirrored camera
int orient = -cameraTexture.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0,0, orient);
}
}
当我使用此代码时,相机的缩放比例非常不同。
我在下面附上了屏幕截图
1.Vuforia焦距预览:: Vuforia Link
2。第二摄像机预览::