Unity3D打开摄像头并且调节分辨率

来源:互联网 发布:软件自动升级原理 编辑:程序博客网 时间:2024/06/05 00:43
using UnityEngine;
using System.Collections;


public class WebCamManager : MonoBehaviour
{


    private string deviceName;
    private WebCamTexture tex;

    private Vector2 resSize = new Vector2(1920, 1080);

// 这里是设置手机的分辨率,可以使摄像更清楚(背景)


    // Use this for initialization
    void Start()
    {
        StartCoroutine(InitCamera());
    }
    // Update is called once per frame
    void Update()
    {
    }
    protected IEnumerator InitCamera()
    {
        //获取授权
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            Debug.Log("devices.Length " + devices.Length);
            if (devices.Length > 0)
            {
                deviceName = devices[0].name;
                Debug.Log("deviceName " + deviceName);
                tex = new WebCamTexture(deviceName, (int)resSize.x, (int)resSize.y, 30);
                Renderer renderer = GetComponent<Renderer>();

                renderer.material.mainTexture = tex;

//将材质贴到plane上面

                tex.Play();
            }
        }
    }

}

将此脚本挂在一个plane上面就可以在上面显示手机摄像头拍摄的东西。

0 0