【问题标题】:Switch device camera in Unity 3d在 Unity 3d 中切换设备相机
【发布时间】:2014-12-27 17:59:14
【问题描述】:

我正在使用 WebCamTexture 类从设备打开相机。我想在游戏之间切换相机从前到后,反之亦然。有人可以帮忙吗?

    WebCamDevice[] devices = WebCamTexture.devices;
    WebCamTexture   webCamTexture = new WebCamTexture(devices[1].name);
    renderer.material.mainTexture = webCamTexture;
    webCamTexture.filterMode = FilterMode.Trilinear;
    webCamTexture.Play();

这会打开我的前置摄像头。但我无法切换相机。

【问题讨论】:

    标签: image unity3d camera crop unityscript


    【解决方案1】:
    if (devices.Length > 1) {
         webCamTexture.Stop();
        if (frontCamera == true)
        {
            webCamTexture.deviceName = devices[0].name;
            frontCamera = WebCamTexture.devices[0].isFrontFacing;
        }
        else
        {
            webCamTexture.deviceName = devices[1].name;
            frontCamera = WebCamTexture.devices[1].isFrontFacing;
        }
         webCamTexture.Play ();
    }
    

    我们已将 Camera Capture Kit 用于社交图像共享游戏/应用程序,以执行与您描述的功能类似的功能,此处介绍的其他解决方案并非 100% 可靠,因为可能存在具有不同顺序的设备正面和背面的设备。

    【讨论】:

      【解决方案2】:

      我已经使用以下代码使用C#脚本切换iPhone设备的前后摄像头

      WebCamDevice[] devices;
      
      public WebCamTexture mCamera = null;
      public GameObject plane; // this is the object where i am going to show the camera
      
      // Use this for initialization
      void Start ()
      {
          devices = WebCamTexture.devices;
      
          plane = GameObject.FindWithTag ("Player");
      
          mCamera = new WebCamTexture (Screen.width,Screen.height);
          mCamera.deviceName = devices[0].name;  // Front camera is at Index 1 and Back Camera is at Index 0
          plane.renderer.material.mainTexture = mCamera;
          mCamera.Play ();
      
      }
      

      上面的代码会在加载场景时显示后置摄像头,点击下面定义的前置按钮即可访问前置摄像头

          if (GUI.Button (new Rect (100, 1000, 120, 40), "Front"))
          {
              if(devices.Length>1)
               {
                  mCamera.Stop();
                  mCamera.deviceName = (mCamera.deviceName == devices[0].name) ? devices[1].name : devices[0].name;
                  mCamera.Play();
              }
      
          }
      

      这是一个按钮,点击它会重定向到前置摄像头,再次点击它会重定向到后置摄像头

      【讨论】:

        【解决方案3】:

        我得到了解决方案。您只需要设置设备的名称。默认情况下,后置摄像头为“Camera 0”和“Camera 1”前置摄像头。首先停止您的相机,更改设置的设备。

        if (devices.Length > 1) {
                 webCamTexture.Stop();
                if (frontCamera == true)
                {
                    webCamTexture.deviceName = devices[0].name;
                    frontCamera = false;
                }
                else
                {
                    webCamTexture.deviceName = devices[1].name;
                    frontCamera = true;
                }
                 webCamTexture.Play ();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-05
          相关资源
          最近更新 更多