关于Unity切换手机前后相机
发表于2017-08-28
现在的智能机基本都有前后摄像头,在使用Unity做开发的时候有时候需要对这前后两个相机进行借还,为此,下面就给大家介绍下在切换手机前后摄像头的处理办法。
- <pre name=< span="">"code" class="csharp"> </pre name=<> copy
- public GUITexture guiTexture;
- private bool isOK;
- private WebCamTexture webcamTexture;
- void Start()
- {
- webcamTexture = new WebCamTexture();
- }
- public void Go()
- {
- //如果有后置摄像头,调用后置摄像头
- for (int i = 0; i < WebCamTexture.devices.Length; i )
- {
- //如果是前置摄像机
- if (WebCamTexture.devices[i].isFrontFacing && isOK)
- {
- webcamTexture.deviceName = WebCamTexture.devices[i].name;
- break;
- }
- //如果是后置摄像机
- else if (!WebCamTexture.devices[i].isFrontFacing && !isOK)
- {
- webcamTexture.deviceName = WebCamTexture.devices[i].name;
- break;
- }
- }
- guiTexture.texture = webcamTexture;
- webcamTexture.Play();
- }
- public void Test()
- {
- if (webcamTexture.isPlaying)
- {
- webcamTexture.Stop();
- }
- }
- void OnGUI()
- {
- if (GUI.Button(new Rect(0,0,200,200),"Front"))
- {
- isOK = true;
- Test();
- Go();
- }
- if (GUI.Button(new Rect(0, 200, 200, 200), "Back"))
- {
- isOK = false;
- Test();
- Go();
- }
- }