Unity 安卓端前后摄像头切换


using UnityEngine;

using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class CameraManager : MonoBehaviour
{
    //显示相机照到的内容
    public Image img;
    //存放按钮
    public List<Button> btns;
    //视频纹理
    WebCamTexture camTexture;
    void Start()
    {
        camTexture = new WebCamTexture();
        foreach (Button btn in btns)
        {
            btn.onClick.AddListener(Change);


        }
        //请求授权在网络播放器中使用网络摄像头
        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 打开前摄像头  false 打开后摄像头
    /// </summary>
    /// <param name="isOpen"></param>
    public void OpenForWardCamera(bool isOpen)
    {
        //检查用户是否已授权在网络播放器中使用网络摄像头
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            if (camTexture != null)
                camTexture.Stop();
            try
            {
                //返回设备列表
                WebCamDevice[] cameraDevices = WebCamTexture.devices;
                if (cameraDevices.Length > 0)
                {


                    //for (int i = 0; i < cameraDevices.Length; i++)
                    //{


                    //如果是前摄像头
                    if (cameraDevices[0].isFrontFacing && isOpen)
                    {
                        //得到设备名字
                        deviceName = cameraDevices[0].name;
                    }
                    else
                    {
                        deviceName = cameraDevices[1].name;
                    }
                    //如果是后摄像头
                    if (!cameraDevices[0].isFrontFacing && !isOpen)
                    {
                        //得到设备名字
                        deviceName = cameraDevices[0].name;
                    }
                    else
                    {
                        deviceName = cameraDevices[1].name;
                    }
                        camTexture = new WebCamTexture(deviceName, Screen.height, Screen.width, 60);
                    //为image设置纹理
                    img.canvasRenderer.SetTexture(camTexture);
                    //启动相机
                    camTexture.Play();
                    // break;
                    //}
                }


            }
            catch (System.Exception e)
            {


                Debug.LogError("Camera No Exit:" + e.ToString());
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值