【Unity3D】调用手机相机

public class UITest : MonoBehaviour
{
    public string deviceName;

    public WebCamTexture webCam;

    // 图片组件
    public RawImage rawImage;

    void Start()
    {
        StartCoroutine(Call());
    }

    public IEnumerator Call()
    {
        // 请求权限
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);

        
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            // 获取相机设备
            WebCamDevice[] devices = WebCamTexture.devices;

            // 取第一个相机名称
            deviceName = devices[0].name;

            // 创建相机贴图
            webCam = new WebCamTexture(deviceName, Screen.width, Screen.height, 60);
            
            // 
            rawImage.texture = webCam;

            // 相机开启
            webCam.Play();
        }
    }
}

最后把UI中的 RawImage 组件拖放到 rawImage 面板中。

 

posted @ 2017-09-16 17:58  三月天的风雪  阅读(2551)  评论(0编辑  收藏  举报