using UnityEngine;
using System.Collections;using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class CameraManager : MonoBehaviour
{
//Display the content of the camera
public Image img;
//Store button
public List<Button> btns ;
//Video texture
WebCamTexture camTexture;
void Start()
{
camTexture = new WebCamTexture();
foreach (Button btn in btns)
{
btn.onClick.AddListener(Change);
}
//Request authorization to use webcam in web player
Application.RequestUserAuthorization(UserAuthorization.WebCam);
}
public void Change()
{
GameObject gob = EventSystem.current.currentSelectedGameObject;
switch (gob.name)
{
case "Close":
Application.Quit();
break;
case "ForWard":
OpenForWardCamera(true);
break;
case "Back":
OpenForWardCamera(false);
break;
default:
break;
}
}
string deviceName;
/// <summary>
/// true to open the front camera false to open the rear camera
/// </summary>
/// <param name="isOpen"></param>
public void OpenForWardCamera(bool isOpen)
{
//Check if the user is authorized to use the webcam in the web player
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (camTexture != null)
camTexture.Stop();
try
{
//return the device list
WebCamDevice[] cameraDevices = WebCamTexture.devices;
if (cameraDevices.Length > 0)
{
//for (int i = 0; i < cameraDevices.Length; i++)
//{
//If it is the front camera
if (cameraDevices[0].isFrontFacing && isOpen)
{
//Get the device name
deviceName = cameraDevices[0]. name;
}
else
{
deviceName = cameraDevices[1].name;
}
//If it is the rear camera
if (!cameraDevices[0].isFrontFacing && !isOpen)
{
//Get the device name
deviceName = cameraDevices[0].name;
}
else
{
deviceName = cameraDevices[1].name;
}
camTexture = new WebCamTexture(deviceName, Screen.height, Screen.width, 60); //Set texture img.canvasRenderer
for image .
SetTexture(camTexture);
//Start the camera
camTexture.Play();
// break;
//}
}
}
catch (System.Exception e)
{
Debug.LogError("Camera No Exit:" + e.ToString());
}
}
}
}