using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Text.RegularExpressions;
using System.IO;
public class TKVideoAndTextureRecordManager : MonoBehaviour
{
public static TKVideoAndTextureRecordManager TKVideoAndTextureRecordManagerInstance;
public RawImage cameraTexture;
public WebCamTexture webCameraTexture;
//public Button SaveButton;
public int captureid = 0;
long[] listLocalVideos;
long currenyListLocalVideosPathID;
public int CurrentItemID;
private void Awake()
{
}
IEnumerator Start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
WebCamDevice[] devices = WebCamTexture.devices;
string devicename = devices[0].name;
webCameraTexture = new WebCamTexture(devicename, Screen.width, Screen.height);
cameraTexture.texture =webCameraTexture;
webCameraTexture.Play();
}
//OnBindEvent();
//SaveButton.enabled = false;
//Debug.LogError("666");
}
启用此本脚本就启用摄像头
private void OnEnable()
{
//if (TKDeviceInformationManagerRef != null)
//{
// captureid = TKDeviceInformationManagerRef.CurrentTextureID;
//}
if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)
{
webCameraTexture.Play();
}
}
//禁用此脚本时停止摄像头
private void OnDisable()
{
if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)
{
webCameraTexture.Stop();
}
}
void Update()
{
//ScreenChange();
}
int width;
/// <summary>
/// 横竖屏切换
/// </summary>
void ScreenChange()
{
if (width == Screen.width)
return;
width = Screen.width;
if (width > Screen.height)
{
cameraTexture.transform.localEulerAngles = Vector3.zero;
}
else
{
cameraTexture.transform.localEulerAngles = new Vector3(0, 0, -90);
}
}
/// <summary>
/// 切换手机前后摄像头
/// </summary>
/// <param name="isOn"></param>
void changeCam(bool isOn)
{
StartCoroutine(CallCamera(isOn));
}
IEnumerator CallCamera(bool isOn)
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
if (webCameraTexture != null)
webCameraTexture.Stop();
WebCamDevice[] cameraDevices = WebCamTexture.devices;
string deviceName = "";
for (int i = 0; i < cameraDevices.Length; i++)
{
//如果是前置摄像机??
if (WebCamTexture.devices[i].isFrontFacing && isOn)
{
deviceName = WebCamTexture.devices[i].name;
TurnCam(isOn);
break;
}
//如果是后置摄像机
else if (!WebCamTexture.devices[i].isFrontFacing && !isOn)
{
deviceName = WebCamTexture.devices[i].name;
TurnCam(isOn);
break;
}
}
webCameraTexture = new WebCamTexture(deviceName, Screen.width, Screen.height);
cameraTexture.texture = webCameraTexture;
webCameraTexture.Play();
}
}
///<summary>
///翻转plane,正确显示摄像头数据
///</summary>
///<param name="isOn">If set to <c>true</c> is turn.</param>
public void TurnCam(bool isOn)
{
#if UNITY_IOS || UNITY_IPHONE
if (!isOn)
cam_Video.rectTransform.localEulerAngles = new Vector3(180, 0, 90);
else cam_Video.rectTransform.localEulerAngles = new Vector3(0, 0, -90);
#elif UNITY_ANDROID
if (!isOn)
cameraTexture.rectTransform.localEulerAngles = new Vector3(0, 0, 0);
else
cameraTexture.rectTransform.localEulerAngles = new Vector3(0, 180, 0);
#endif
}
}