关于Unity切换手机前后相机

发表于2017-08-28
评论0 1.9k浏览
现在的智能机基本都有前后摄像头,在使用Unity做开发的时候有时候需要对这前后两个相机进行借还,为此,下面就给大家介绍下在切换手机前后摄像头的处理办法。
  1. <pre name=< span="">"code" class="csharp">  </pre name=<> copy

  1. public GUITexture guiTexture;  
  2.   
  3.    private bool isOK;  
  4.    private WebCamTexture webcamTexture;  
  5.    void Start()  
  6.    {  
  7.   
  8.      webcamTexture  = new WebCamTexture();  
  9.    }  
  10.   
  11.    public void Go()  
  12.    {  
  13.   
  14.        //如果有后置摄像头,调用后置摄像头    
  15.        for (int i = 0; i < WebCamTexture.devices.Length; i )  
  16.        {  
  17.            //如果是前置摄像机  
  18.            if (WebCamTexture.devices[i].isFrontFacing && isOK)  
  19.            {  
  20.                webcamTexture.deviceName = WebCamTexture.devices[i].name;  
  21.                break;  
  22.            }  
  23.            //如果是后置摄像机  
  24.            else if (!WebCamTexture.devices[i].isFrontFacing && !isOK)  
  25.            {  
  26.                webcamTexture.deviceName = WebCamTexture.devices[i].name;  
  27.                break;  
  28.            }  
  29.        }  
  30.        guiTexture.texture = webcamTexture;  
  31.        webcamTexture.Play();  
  32.    }  
  33.   
  34.    public void Test()  
  35.    {  
  36.        if (webcamTexture.isPlaying)  
  37.        {  
  38.            webcamTexture.Stop();  
  39.        }  
  40.    }  
  41.   
  42.    void OnGUI()  
  43.    {  
  44.        if (GUI.Button(new Rect(0,0,200,200),"Front"))  
  45.        {  
  46.            isOK = true;  
  47.            Test();  
  48.            Go();  
  49.        }  
  50.        if (GUI.Button(new Rect(0, 200, 200, 200), "Back"))  
  51.        {  
  52.            isOK = false;  
  53.            Test();  
  54.            Go();  
  55.        }  
  56.    }  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: