更一般地说,如果一台设备有两台摄像机,当从应用程序中选择摄像机时如何访问前置摄像机,是否有办法初始化其中一台摄像机?
这是我为相机编写的代码。
这是我的摄像机控制脚本
using UnityEngine;
using System.Collections;
public class CameraPlaneController : MonoBehaviour {
public Camera _targetCam;
ScreenOrientation orientation;
float height = 0;
float width = 0;
// Use this for initialization
void Awake () {
float Screenheight = (float)_targetCam.orthographicSize* 2.0f;
float Screenwidth = Screenheight * Screen.width / Screen.height;
height = Screenheight ;
width = Screenwidth;
this.transform.localPosition = new Vector3(0,0,91.6f);
#if UNITY_EDITOR
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#elif UNITY_WEBPLAYER
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#endif
orientation = Screen.orientation;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
if (Screen.orientation == ScreenOrientation.Portrait||
Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
#if UNITY_EDITOR
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#elif UNITY_ANDROID
transform.localEulerAngles = new Vector3(0,270,90);
transform.localScale = new Vector3(height/10, 1.0f, width/10);
#elif UNITY_IOS
if( Screen.orientation == ScreenOrientation.PortraitUpsideDown)
{
transform.localEulerAngles = new Vector3(0,270,90);
}
else
{
transform.localEulerAngles = new Vector3(0,90,270);
}
transform.localScale = new Vector3(-1*height/10, 1.0f, width/10);
#endif
} else if (Screen.orientation == ScreenOrientation.Landscape) {
#if UNITY_EDITOR
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#elif UNITY_ANDROID
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#elif UNITY_IOS
transform.localEulerAngles = new Vector3(-90,0,0);
transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
#endif
}
}
// Update is called once per frame
void Update () {
if (orientation != Screen.orientation) {
int screenHeight_1 = Screen.height;
int screenWidth_1 = Screen.width;
if (Screen.orientation == ScreenOrientation.Portrait||
Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
if(screenHeight_1 < screenWidth_1)
{
int tempvalue = screenWidth_1;
screenWidth_1 = screenHeight_1;
screenHeight_1 = tempvalue;
}
float Screenheight = (float)_targetCam.orthographicSize* 2.0f;
float Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
height = Screenheight ;
width = Screenwidth;
#if UNITY_ANDROID
transform.localEulerAngles = new Vector3(0,270,90);
transform.localScale = new Vector3(height/10, 1.0f, width/10);
#elif UNITY_IOS
if( Screen.orientation == ScreenOrientation.PortraitUpsideDown)
{
transform.localEulerAngles = new Vector3(0,270,90);
}
else
{
transform.localEulerAngles = new Vector3(0,90,270);
}
transform.localScale = new Vector3(-1*height/10, 1.0f, width/10);
#endif
} else if (Screen.orientation == ScreenOrientation.Landscape||
Screen.orientation == ScreenOrientation.LandscapeLeft) {
if(screenHeight_1 > screenWidth_1)
{
int tempvalue = screenWidth_1;
screenWidth_1 = screenHeight_1;
screenHeight_1 = tempvalue;
}
float Screenheight = (float)_targetCam.orthographicSize* 2.0f;
float Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
height = Screenheight ;
width = Screenwidth;
#if UNITY_ANDROID
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#elif UNITY_IOS
transform.localEulerAngles = new Vector3(-90,0,0);
transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
#endif
}
else if(Screen.orientation == ScreenOrientation.LandscapeRight)
{
if(screenHeight_1 > screenWidth_1)
{
int tempvalue = screenWidth_1;
screenWidth_1 = screenHeight_1;
screenHeight_1 = tempvalue;
}
float Screenheight = (float)_targetCam.orthographicSize* 2.0f;
float Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
height = Screenheight ;
width = Screenwidth;
#if UNITY_ANDROID
transform.localEulerAngles = new Vector3(-90,0,0);
transform.localScale = new Vector3(width/10, 1.0f, height/10);
#elif UNITY_IOS
transform.localEulerAngles = new Vector3(90,180,0);
transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
#endif
}
orientation = Screen.orientation;
}
}
}
这是我的设备相机控制脚本
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DeviceCameraController : MonoBehaviour {
public enum CameraMode
{
FACE_C,
DEFAULT_C,
NONE
}
[HideInInspector]
public WebCamTexture cameraTexture;
private bool isPlay = false;
//public CameraMode e_CameraMode;
GameObject e_CameraPlaneObj;
int matIndex = 0;
ScreenOrientation orientation;
public bool isPlaying
{
get{
return isPlay;
}
}
// Use this for initialization
void Awake()
{
StartCoroutine(CamCon());
e_CameraPlaneObj = transform.FindChild ("CameraPlane").gameObject;
}
// Update is called once per frame
void Update()
{
if (isPlay) {
if(e_CameraPlaneObj.activeSelf)
{
e_CameraPlaneObj.GetComponent<Renderer>().material.mainTexture = cameraTexture;
}
}
}
IEnumerator CamCon()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
#if UNITY_EDITOR_WIN
cameraTexture = new WebCamTexture();
#elif UNITY_EDITOR_OSX
cameraTexture = new WebCamTexture(960,640);
#elif UNITY_IOS
cameraTexture = new WebCamTexture(960,640);
#elif UNITY_ANDROID
cameraTexture = new WebCamTexture(960,640);
#else
cameraTexture = new WebCamTexture();
#endif
cameraTexture.Play();
isPlay = true;
}
}
public void StopWork()
{
this.cameraTexture.Stop();
}
}
答案 0 :(得分:3)
创建新的WebCamTexture而不提供设备名称会自动使用Unity找到的第一台摄像头。相反,您需要找到前置摄像头的设备名称并使用它。
WebCamTexture有一个名为devices
的静态变量,可让您遍历连接到计算机的所有已知摄像头。每个设备都有一个布尔isFrontFacing
,告诉你摄像机面向哪个方向。在相机初始化中,您可以执行以下操作:
string selectedDeviceName = "";
WebCamDevices[] allDevices = WebCamTexture.devices;
for(int i = 0; i < allDevices.Length; i++)
{
if (allDevices[i].isFrontFacing)
{
selectedDeviceName = allDevices[i].name;
break;
}
}
this.cameraTexture = new WebCamTexture(selectedDeviceName, 960, 640);
这适用于大多数情况。我注意到有些相机没有列出设备名称,这意味着您无法使用该设备构建新的WebCamTexture
。其他相机无法正确报告它们是否正面。据我所知,处理这些案件并不是一个好方法。