如何在Unity中从设备中选择前置摄像头

时间:2021-02-10 20:15:23

标签: c# android unity3d android-camera

嗯,我对 Unity 还是个新手,对纹理的真正菜鸟,但我设法使用了设备的相机(使用本文末尾的代码)

问题是我想在使用智能手机设备时使用前置摄像头。 有没有菜鸟的方法?

感谢您的关注、时间和帮助<3

using UnityEngine;
using UnityEngine.UI;

public class CameraScript : MonoBehaviour
{
    static WebCamTexture backCam;

    void Start()
    {
        if (backCam == null)
            backCam = new WebCamTexture();

        GetComponent<Renderer>().material.mainTexture = backCam;

        this.GetComponent<RawImage>().material = this.GetComponent<SpriteRenderer>().material;

        if (!backCam.isPlaying)
            backCam.Play();

    } 
}

1 个答案:

答案 0 :(得分:0)

这是您在 Unity 中使用前置摄像头的方式:

public class CameraScript : MonoBehaviour
{    
    WebCamTexture = null;

    void Start()
    {
       WebCamDevice[] devices = WebCamTexture.devices;

       foreach (var device in devices) {
         if (device.isFrontFacing) {
           webCamTexture = new WebCamTexture(device.name);
         }
       }
 
       if (webCamTexture == null) {
         webCamTexture = new WebCamTexture();
       }
    }
}

如果没有前置摄像头可用,那么在我提供的示例中,WebCamDevice 默认为空构造函数 WebCamTexture

最好参考 Unity 文档,尽管它并不总是最好的文档。

这是 WebCamTexture 文档。

这是 WebCamDevice 文档。

Unity forums 还包含丰富的信息,根据问题,Unity 引擎开发人员有时会回答问题。

相关问题