unity 启动相机_Unity打开摄像头

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;

///

/// 横竖屏切换

///

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);

}

}

///

/// 切换手机前后摄像头

///

///

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();

}

}

///

///翻转plane,正确显示摄像头数据

///

///If set to true is turn.

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

}

}

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.SceneManagement;

using System.Text.RegularExpressions;

using System.IO;

using cn.sharerec;

public class TKVideoAndTextureRecordManager : TKUIManager

{

public static TKVideoAndTextureRecordManager TKVideoAndTextureRecordManagerInstance;

public Text DeviceNameText;

public RawImage cameraTexture;

public WebCamTexture webCameraTexture;

public Button ReturnButton;

//public Button SaveButton;

public Button ViedoButton;

public Button CaptureButton;

public Button CameraSwitchButton;

public Toggle changeCameraToggle;

public Toggle VideoToggle;

public ShareREC ShareRECRef;

public int captureid=0;

public TKDeviceInformationManager TKDeviceInformationManagerRef;

public Text Videotext;

long[] listLocalVideos;

long currenyListLocalVideosPathID;

public int CurrentItemID;

private void Awake()

{

OnInit();

DeviceNameText.text= TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[TKGlobalData.currentSelectEquiID].equiName;

TKGlobalData.CurrentTextureList = new List();

}

//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();

// }

// //SaveButton.enabled = false;

//}

private void OnSetVideoPath()

{

ShareRECRef.CacheFolder =TKGlobalData.CurrentSaveDataPath + TKDataHandle.TKDataHandleInstance.CurrentWorkShops[TKGlobalData.currentSelectWorkShopID].workShopName;

ShareRECImpl.SetCacheFolder(ShareRECRef.CacheFolder);

}

启用此本脚本就启用摄像头

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;

///

/// 横竖屏切换

///

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);

}

}

///

/// 切换手机前后摄像头

///

///

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();

}

}

///

///翻转plane,正确显示摄像头数据

///

///If set to true is turn.

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

}

public override void OnInit()

{

OnBindEvent();

}

public override void OnBindEvent()

{

base.OnBindEvent();

EventTriggerListener.Get(ReturnButton.gameObject).onClick += OnReturnButtonClick;

//EventTriggerListener.Get(SaveButton.gameObject).onClick += OnSaveButtonClick;

//SaveButton.onClick.AddListener(delegate() { OnSaveButtonClick(SaveButton.gameObject);});

//EventTriggerListener.Get(ViedoButton.gameObject).onClick += OnViedoButtonClick;

//EventTriggerListener.Get(CaptureButton.gameObject).onClick += OnCaptureButtonClick;

CaptureButton.onClick.AddListener(delegate() { OnCaptureButtonClick(CaptureButton.gameObject);});

EventTriggerListener.Get(CameraSwitchButton.gameObject).onClick += OnCameraSwitchButtonClick;

changeCameraToggle.onValueChanged.AddListener(changeCam);

VideoToggle.onValueChanged.AddListener(OnVideoToggleChange);

VideoToggle.onValueChanged.AddListener((bool value) => OnVideoToggleChange(VideoToggle,value));

}

void OnVideoToggleChange(Toggle clickToggle, bool isOn)

{

if (isOn)

{

Videotext.text=”开始”+ShareREC.IsAvailable();

//Debug.Log(isOn);

if (ShareREC.IsAvailable())

{

//OnSetVideoPath();//更改视频保存路径失败

//Debug.Log(“开始录制!”);

ShareREC.StartRecorder();

ShareREC.SetText(“hahahaha”);

ShareREC.AddCustomAttr(“test”,”nihao”);

}

}

else

{

Videotext.text = “停止” + ShareREC.IsAvailable();

//Debug.Log(isOn);

if (ShareREC.IsAvailable())

{

//ShareREC.ShowProfile();

//ShareREC.OnRecorderStoppedHandler = OnEndRecord;

ShareREC.StopRecorder();

ShareREC.SetText(“heihei”);

ShareREC.AddCustomAttr(“test2”, “nihao2”);

TKDeviceInformationManagerRef.LocalVideoID++;

listLocalVideos = ShareREC.ListLocalVideos();

currenyListLocalVideosPathID = listLocalVideos[listLocalVideos.Length – 1];

string videoPath = ShareREC.GetLocalVideoPath(currenyListLocalVideosPathID);

TKDeviceInformationManagerRef.AddCurrentVideoDIRList( videoPath,TKDeviceInformationManagerRef.LocalVideoID.ToString());

}

}

}

//void OnEndRecord()

//{

// Debug.Log( “录制结束”);

//}

private void OnReturnButtonClick(GameObject go)

{

if (go == ReturnButton.gameObject)

{

OnReturnButton();

}

}

private void OnReturnButton()

{

//Debug.Log(“ReturnButton……”);

Videotext.text = “结束,找不到相机” + ShareREC.IsAvailable();

//Debug.Log(isOn);

if (ShareREC.IsAvailable())

{

//ShareREC.ShowProfile();

// ShareREC.OnRecorderStoppedHandler = OnEndRecord;

ShareREC.StopRecorder();

}

TKUIStateSelect.TKUIStateSelectInstance.OnSetCurrentUiPanelsState(TKPanelType.DeviceInformationPanel);

TKUIStateSelect.TKUIStateSelectInstance.OnSetCurrentUiPanelsState(TKPanelType.DeviceInformationPanelParent);

//TKDeviceInformationManagerRef.CurrentTextureID = captureid;

}

///

/// 保存功能去掉2019.4.3

///

///

//private void OnSaveButtonClick(GameObject go)

// {

// if (go == SaveButton.gameObject)

// {

// OnSaveButton();

// }

// }

// private void OnSaveButton()

// {

// Debug.Log(“SaveButton……”);

// captureid–;

// TKCaptureAndVideoRecord.TKCaptureAndVideoRecordInsrance.CaptureSave(TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[0].equiName+captureid,TKDataHandle.TKDataHandleInstance.CurrentWorkShops[0].workShopName);

// if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)

// {

// webCameraTexture.Play();

// }

// SaveButton.enabled = false;

// }

private void OnViedoButtonClick(GameObject go)

{

if (go == ViedoButton.gameObject)

{

OnViedoButton();

}

}

private void OnViedoButton()

{

Debug.Log(“ViedoButton……”);

//TKCaptureAndVideoRecord.TKCaptureAndVideoRecordInsrance.VideoTape();//此方法会截屏一大堆图片

}

private void OnCaptureButtonClick(GameObject go)

{

if (go == CaptureButton.gameObject)

{

OnCaptureButton();

}

}

private void OnCaptureButton()

{

//Debug.Log(“CaptureButton……”);

//captureid++;

TKDeviceInformationManagerRef.CurrentTextureID++;

TKCaptureAndVideoRecord.TKCaptureAndVideoRecordInsrance.CaptureSave(TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[TKGlobalData.currentSelectEquiID].equiId+”_”+TKDeviceInformationManagerRef.CurrentTextureID,TKDataHandle.TKDataHandleInstance.CurrentWorkShops[TKGlobalData.currentSelectWorkShopID].workShopName);

//TKGlobalData.CurrentTextureList.Add(TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[TKGlobalData.currentSelectEquiID].equiId + “_” + captureid);

TKDeviceInformationManagerRef.currentDeviceTextureList.Add(TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[TKGlobalData.currentSelectEquiID].equiId + “_” + TKDeviceInformationManagerRef.CurrentTextureID);

TKDeviceInformationManagerRef.currentDeviceTextureNameList.Add(TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[TKGlobalData.currentSelectEquiID].equiName+”_”+TKDeviceInformationManagerRef.CurrentTextureID );

//TKCaptureAndVideoRecord.TKCaptureAndVideoRecordInsrance.CaptureSave(TKDataHandle.TKDataHandleInstance.CurrentEquiInfos[0].equiName + captureid);

///暂时这总点击一次拍摄如果不保存,第二次点击继续拍摄(不保存的功能去掉)2019.4.3

//if (captureid % 2 ==1)

//{

// SaveButton.enabled = true;

// if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)

// {

// webCameraTexture.Stop();

// }

//}

//else

//{

// SaveButton.enabled = false;

// if (Application.HasUserAuthorization(UserAuthorization.WebCam) && webCameraTexture != null)

// {

// webCameraTexture.Play();

// }

//}

}

private void OnCameraSwitchButtonClick(GameObject go)

{

if (go == CameraSwitchButton.gameObject)

{

OnCameraSwitchButton();

}

}

private void OnCameraSwitchButton()

{

Debug.Log(“CameraSwitchButton……”);

}

}

    原文作者:梦醒方如初24
    原文地址: https://blog.csdn.net/weixin_30853127/article/details/112023465
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞