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

猜你喜欢

转载自blog.csdn.net/qq_23231303/article/details/80365601
今日推荐