unity调用摄像头

      在unity调用设备的摄像头显示在GUI界面中,在pc和android手机中都适用

     代码如下


using UnityEngine;
using System.Collections;

public class CameraTest : MonoBehaviour
{
    public WebCamTexture cameraTexture;
    public string cameraName = "";
    private bool isPlay = false;
    // Use this for initialization  
    void Start()
    {
        StartCoroutine(Test());
    }

    // Update is called once per frame  
    void Update()
    {

    }

    IEnumerator Test()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            cameraTexture = new WebCamTexture(cameraName, 400, 300, 15);
            cameraTexture.Play();
            isPlay = true;
        }
    }

    void OnGUI()
    {
        if (isPlay)
        {
            GUI.DrawTexture(new Rect(0, 0, 400, 300), cameraTexture, ScaleMode.ScaleToFit);
        }
    }
}

     但是有个问题,比如我只想让摄像头显示在场景中的某一块,或者是在一个物体上

public Material webCamShow;
webCamShow.mainTexture=cameraTexture; 
    可以先定义一个材质,然后让它的纹理为cameraTexture。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过 Unity 的 WebCamTexture 类来调用摄像头拍照。以下是一个简单的示例代码: ``` using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { private WebCamTexture webcamTexture; // Start is called before the first frame update void Start() { // 获取摄像头设备 WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length == 0) { Debug.Log("No camera detected!"); return; } // 使用第一个摄像头设备 webcamTexture = new WebCamTexture(devices[0].name); // 开始捕捉摄像头画面 webcamTexture.Play(); } // 拍照方法 public void TakePhoto() { // 创建 Texture2D 对象 Texture2D photo = new Texture2D(webcamTexture.width, webcamTexture.height); // 从摄像头捕捉画面到 Texture2D 对象 photo.SetPixels(webcamTexture.GetPixels()); photo.Apply(); // 保存照片到本地 byte[] bytes = photo.EncodeToPNG(); System.IO.File.WriteAllBytes(Application.dataPath + "/photo.png", bytes); } // 在场景中显示摄像头画面 void OnGUI() { if (webcamTexture != null && webcamTexture.isPlaying) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), webcamTexture, ScaleMode.ScaleToFit); } } // Stop capturing webcam texture when the script is disabled or destroyed void OnDisable() { if (webcamTexture != null) { webcamTexture.Stop(); } } void OnDestroy() { if (webcamTexture != null) { webcamTexture.Stop(); } } } ``` 在 Start() 方法中,我们获取设备上的摄像头设备,并使用第一个设备。然后通过调用 `WebCamTexture.Play()` 方法开始捕捉摄像头画面。 在 TakePhoto() 方法中,我们创建一个 Texture2D 对象,然后从摄像头捕捉画面到 Texture2D 对象中。最后,我们将照片保存到本地。 在 OnGUI() 方法中,我们通过调用 `GUI.DrawTexture()` 方法来显示摄像头画面。 最后,在 OnDisable() 和 OnDestroy() 方法中,我们停止捕捉摄像头画面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值