unity 启动相机_Unity打开摄像头

这篇博客详细介绍了如何在Unity中使用C#脚本来启动、切换和控制手机摄像头,包括横竖屏切换、前后摄像头的切换以及摄像头数据的正确显示。此外,还涉及到了视频录制的相关操作。
摘要由CSDN通过智能技术生成

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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值