C# 在unity中打开摄像头应用程序时如何访问设备的前摄像头

C# 在unity中打开摄像头应用程序时如何访问设备的前摄像头,c#,unity3d,camera,unity5,C#,Unity3d,Camera,Unity5,更一般地说,如果一个设备有两个摄像头,当从应用程序中选择摄像头时,如何访问前置摄像头,是否有任何方法可以初始化其中一个摄像头 这是我为照相机编写的代码 这是我的相机平面控制脚本 using UnityEngine; using System.Collections; public class CameraPlaneController : MonoBehaviour { public Camera _targetCam; ScreenOrienta

更一般地说,如果一个设备有两个摄像头,当从应用程序中选择摄像头时,如何访问前置摄像头,是否有任何方法可以初始化其中一个摄像头

  • 这是我为照相机编写的代码

  • 这是我的相机平面控制脚本

        using UnityEngine;
        using System.Collections;
    
        public class CameraPlaneController : MonoBehaviour {
    
        public Camera _targetCam;
    
        ScreenOrientation orientation;
        float height = 0;
        float width = 0;
    
    
        // Use this for initialization
        void Awake () {
    
        float Screenheight = (float)_targetCam.orthographicSize* 2.0f; 
        float Screenwidth = Screenheight * Screen.width / Screen.height;
        height = Screenheight ;
        width = Screenwidth;
        this.transform.localPosition = new Vector3(0,0,91.6f);
    
        #if UNITY_EDITOR
        transform.localEulerAngles = new Vector3(90,180,0);
        transform.localScale = new Vector3(width/10, 1.0f, height/10);
        #elif UNITY_WEBPLAYER
        transform.localEulerAngles = new Vector3(90,180,0);
        transform.localScale = new Vector3(width/10, 1.0f, height/10);
        #endif
    
        orientation = Screen.orientation;
    
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        if (Screen.orientation == ScreenOrientation.Portrait||
            Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    
            #if UNITY_EDITOR
            transform.localEulerAngles = new Vector3(90,180,0);
            transform.localScale = new Vector3(width/10, 1.0f, height/10);
    
            #elif UNITY_ANDROID
            transform.localEulerAngles = new Vector3(0,270,90);
            transform.localScale = new Vector3(height/10, 1.0f, width/10);
            #elif UNITY_IOS
            if( Screen.orientation == ScreenOrientation.PortraitUpsideDown)
            {
                transform.localEulerAngles = new Vector3(0,270,90);
            }
            else
            {
                transform.localEulerAngles = new Vector3(0,90,270);
            }
            transform.localScale = new Vector3(-1*height/10, 1.0f, width/10);
            #endif
        } else if (Screen.orientation == ScreenOrientation.Landscape) {
            #if UNITY_EDITOR
            transform.localEulerAngles = new Vector3(90,180,0);
            transform.localScale = new Vector3(width/10, 1.0f, height/10);
    
            #elif UNITY_ANDROID
            transform.localEulerAngles = new Vector3(90,180,0);
            transform.localScale = new Vector3(width/10, 1.0f, height/10);
            #elif UNITY_IOS
            transform.localEulerAngles = new Vector3(-90,0,0);
            transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
    
            #endif
        }
        }
    
        // Update is called once per frame
        void Update () {
        if (orientation != Screen.orientation) {
    
            int screenHeight_1 = Screen.height;
            int screenWidth_1 = Screen.width;
            if (Screen.orientation == ScreenOrientation.Portrait||
                Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
    
                if(screenHeight_1 < screenWidth_1)
                {
                    int tempvalue = screenWidth_1;
                    screenWidth_1 = screenHeight_1;
                    screenHeight_1 = tempvalue;
                }
                float Screenheight = (float)_targetCam.orthographicSize* 2.0f; 
                float Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
                height = Screenheight ;
                width = Screenwidth;
                #if UNITY_ANDROID
                transform.localEulerAngles = new Vector3(0,270,90);
                transform.localScale = new Vector3(height/10, 1.0f, width/10);
                #elif UNITY_IOS
                if( Screen.orientation == ScreenOrientation.PortraitUpsideDown)
                {
                    transform.localEulerAngles = new Vector3(0,270,90);
                }
                else
                {
                    transform.localEulerAngles = new Vector3(0,90,270);
                }
    
                transform.localScale = new Vector3(-1*height/10, 1.0f, width/10);
                #endif
            } else if (Screen.orientation == ScreenOrientation.Landscape||
                       Screen.orientation == ScreenOrientation.LandscapeLeft) {
    
                if(screenHeight_1 > screenWidth_1)
                {
                    int tempvalue = screenWidth_1;
                    screenWidth_1 = screenHeight_1;
                    screenHeight_1 = tempvalue;
                }
    
                float Screenheight = (float)_targetCam.orthographicSize* 2.0f; 
                float Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
                height = Screenheight ;
                width = Screenwidth;
    
                #if UNITY_ANDROID
                transform.localEulerAngles = new Vector3(90,180,0);
                transform.localScale = new Vector3(width/10, 1.0f, height/10);
                #elif UNITY_IOS
                transform.localEulerAngles = new Vector3(-90,0,0);
                transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
                #endif
            }
            else if(Screen.orientation == ScreenOrientation.LandscapeRight)
            {
                if(screenHeight_1 > screenWidth_1)
                {
                    int tempvalue = screenWidth_1;
                    screenWidth_1 = screenHeight_1;
                    screenHeight_1 = tempvalue;
                }
    
                float Screenheight = (float)_targetCam.orthographicSize* 2.0f; 
                float Screenwidth = Screenheight * screenWidth_1 / screenHeight_1;
                height = Screenheight ;
                width = Screenwidth;
                #if UNITY_ANDROID
                transform.localEulerAngles = new Vector3(-90,0,0);
                transform.localScale = new Vector3(width/10, 1.0f, height/10);
                #elif UNITY_IOS
    
                transform.localEulerAngles = new Vector3(90,180,0);
                transform.localScale = new Vector3(-1*width/10, 1.0f, height/10);
                #endif
            }
            orientation = Screen.orientation;
        }
        }
    
    
        }
    
    使用UnityEngine;
    使用系统集合;
    公共类CameraPlaneController:MonoBehavior{
    公共摄像机;
    屏幕定位;
    浮动高度=0;
    浮动宽度=0;
    //用于初始化
    无效唤醒(){
    浮动屏幕高度=(浮动)_targetCam.正交尺寸*2.0f;
    浮动屏幕宽度=屏幕高度*屏幕宽度/屏幕高度;
    高度=屏幕高度;
    宽度=屏幕宽度;
    this.transform.localPosition=新向量3(0,0,91.6f);
    #如果统一编辑器
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #elif UNITY_网络播放器
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #恩迪夫
    方向=屏幕方向;
    Screen.sleepTimeout=sleepTimeout.NeverSleep;
    如果(Screen.orientation==ScreenOrientation.Grait||
    Screen.orientation==ScreenOrientation.graphicalupsidedown){
    #如果统一编辑器
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #艾利夫·尤尼安卓
    transform.localEulerAngles=新向量3(0270,90);
    transform.localScale=新矢量3(高度/10,1.0f,宽度/10);
    #伊里夫·尤尼奥斯
    if(Screen.orientation==ScreenOrientation.graphicalupsidedown)
    {
    transform.localEulerAngles=新向量3(0270,90);
    }
    其他的
    {
    transform.localEulerAngles=新向量3(0,90270);
    }
    transform.localScale=新矢量3(-1*height/10,1.0f,width/10);
    #恩迪夫
    }else if(Screen.orientation==ScreenOrientation.scape){
    #如果统一编辑器
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #艾利夫·尤尼安卓
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #伊里夫·尤尼奥斯
    transform.localEulerAngles=新向量3(-90,0,0);
    transform.localScale=新矢量3(-1*width/10,1.0f,height/10);
    #恩迪夫
    }
    }
    //每帧调用一次更新
    无效更新(){
    if(方向!=屏幕方向){
    int screenHeight_1=Screen.height;
    int screenWidth_1=Screen.width;
    如果(Screen.orientation==ScreenOrientation.Grait||
    Screen.orientation==ScreenOrientation.graphicalupsidedown){
    if(屏幕高度_1<屏幕宽度_1)
    {
    int tempvalue=屏幕宽度_1;
    屏幕宽度_1=屏幕高度_1;
    屏幕高度_1=温度值;
    }
    浮动屏幕高度=(浮动)_targetCam.正交尺寸*2.0f;
    浮动屏幕宽度=屏幕高度*屏幕宽度_1/屏幕高度_1;
    高度=屏幕高度;
    宽度=屏幕宽度;
    #如果你是安卓
    transform.localEulerAngles=新向量3(0270,90);
    transform.localScale=新矢量3(高度/10,1.0f,宽度/10);
    #伊里夫·尤尼奥斯
    if(Screen.orientation==ScreenOrientation.graphicalupsidedown)
    {
    transform.localEulerAngles=新向量3(0270,90);
    }
    其他的
    {
    transform.localEulerAngles=新向量3(0,90270);
    }
    transform.localScale=新矢量3(-1*height/10,1.0f,width/10);
    #恩迪夫
    }else if(Screen.orientation==ScreenOrientation.scape||
    Screen.orientation==ScreenOrientation.LandscapeLeft){
    如果(屏幕高度_1>屏幕宽度_1)
    {
    int tempvalue=屏幕宽度_1;
    屏幕宽度_1=屏幕高度_1;
    屏幕高度_1=温度值;
    }
    浮动屏幕高度=(浮动)_targetCam.正交尺寸*2.0f;
    浮动屏幕宽度=屏幕高度*屏幕宽度_1/屏幕高度_1;
    高度=屏幕高度;
    宽度=屏幕宽度;
    #如果你是安卓
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #伊里夫·尤尼奥斯
    transform.localEulerAngles=新向量3(-90,0,0);
    transform.localScale=新矢量3(-1*width/10,1.0f,height/10);
    #恩迪夫
    }
    else if(Screen.orientation==ScreenOrientation.LandscapeRight)
    {
    如果(屏幕高度_1>屏幕宽度_1)
    {
    int tempvalue=屏幕宽度_1;
    屏幕宽度_1=屏幕高度_1;
    屏幕高度_1=温度值;
    }
    浮动屏幕高度=(浮动)_targetCam.正交尺寸*2.0f;
    浮动屏幕宽度=屏幕高度*屏幕宽度_1/屏幕高度_1;
    高度=屏幕高度;
    宽度=屏幕宽度;
    #如果你是安卓
    transform.localEulerAngles=新向量3(-90,0,0);
    transform.localScale=新矢量3(宽度/10,1.0f,高度/10);
    #伊里夫·尤尼奥斯
    transform.localEulerAngles=新向量3(90180,0);
    transform.localScale=新矢量3(-1*width/10,1.0f,height/10);
    #恩迪夫
    }
    方向=屏幕方向;
    }
    }
    }
    
    这是我的设备摄像头控制脚本

        using UnityEngine;
        using UnityEngine.UI;
        using System.Collections;
        public class DeviceCameraController : MonoBehaviour {
    
    public enum CameraMode
    {
        FACE_C,
        DEFAULT_C,
        NONE
    }
    [HideInInspector]
    public WebCamTexture cameraTexture; 
    
    private bool isPlay = false;
    //public CameraMode e_CameraMode;
    GameObject e_CameraPlaneObj;
    int matIndex = 0;
    
    ScreenOrientation orientation;
    public bool isPlaying
    {
        get{
            return isPlay;
        }
    }
    // Use this for initialization  
    void Awake()  
    {  
        StartCoroutine(CamCon());  
        e_CameraPlaneObj = transform.FindChild ("CameraPlane").gameObject;
    
    }
    
    // Update is called once per frame  
    void Update()  
    {  
        if (isPlay) {  
            if(e_CameraPlaneObj.activeSelf)
            {
                e_CameraPlaneObj.GetComponent<Renderer>().material.mainTexture = cameraTexture;
            }
    
        }
    }
    
    IEnumerator CamCon()  
    {  
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);  
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))  
        {  
            #if UNITY_EDITOR_WIN 
            cameraTexture = new WebCamTexture();  
            #elif UNITY_EDITOR_OSX
            cameraTexture = new WebCamTexture(960,640);  
            #elif UNITY_IOS
            cameraTexture = new WebCamTexture(960,640);
            #elif UNITY_ANDROID
            cameraTexture = new WebCamTexture(960,640);  
    
            #else
            cameraTexture = new WebCamTexture(); 
            #endif
            cameraTexture.Play();
            isPlay = true;  
        }  
    }
    
    public void StopWork()
    {
        this.cameraTexture.Stop();
    }
    
    }
    
    使用Un
    
    string selectedDeviceName = "";
    WebCamDevices[] allDevices = WebCamTexture.devices;
    for(int i = 0; i < allDevices.Length; i++)
    {
        if (allDevices[i].isFrontFacing)
        {
            selectedDeviceName = allDevices[i].name;
            break;
        }
    }
    
    this.cameraTexture = new WebCamTexture(selectedDeviceName, 960, 640);