Reputation: 521
More generally, If a device has two camera,how to acess the frontcamera when camera is selected from app, is there any way to initialize one of them in particular?
this is my code that i have written for camera.
this is my script for camera plane controle
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;
}
}
}
this is my script for device camera controle
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();
}
}
Upvotes: 0
Views: 3251
Reputation: 236
Creating a new WebCamTexture without providing a device name automatically uses the first camera Unity finds. Instead, you need to find the device name of the front facing camera and use that.
WebCamTexture has a static variable called devices
that lets you iterate through all the known cameras connected to the computer. Each device has a boolean isFrontFacing
which tells you which way the camera is facing. In your camera initialization you can do this:
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);
This will work in the majority of cases. I have noticed some cameras do not list a device name, which means you can't construct a new WebCamTexture
with that device. Other cameras do not correctly report if they are front-facing. As far as I know there isn't a good way to handle these cases.
Upvotes: 4