U3D实现WebCamera显示
发布日期:2021-05-15 16:03:30 浏览次数:10 分类:精选文章

本文共 3274 字,大约阅读时间需要 10 分钟。

������������Unity3D������WebCamera���������������������

���������Unity3D������������WebCamera���������������������������������������������������������������

1. ������������������

���Unity3D������������������������������������

  • WebCamDevice���������������������������������������������
  • WebCamTexture������������������������������������������������

2. ������������

���������������

  • ������������ Resources ������������������������������ Material ������������
  • ��� Material ������������������ CameraPlane.mat ���������������������������Shader��� Unlit/Texture���

3. ���������������������

������������������������ Camera ��������������������������� WebCamera������ WebCamera ������������������������������ Plane��������������� Plane ��� Yield���

  • Plane���Rotation��������������� Rotation (X: 90, Y: 180, Z: 0)���������������������������������
  • ������������������������������������������ CameraPlane.mat ������������������ Plane ��� MeshRenderer ������

4. ���������������������������

��� WebCamera ��������������� WebCameraManager.cs ���������������������������������������������������������

  • WebCameraManager.cs ���������������������
using System.Collections;using System.Collections.Generic;using UnityEngine;public class WebCameraManager : MonoBehaviour{    public string DeviceName;    public Vector2 CameraSize;    public float CameraFPS;    public WebCamTexture _webCamera;    public GameObject Plane;    void OnGUI()    {        if (GUI.Button(new Rect(100, 100, 100, 100), "Initialize Camera"))        {            StartCoroutine("InitCameraCor");        }        if (GUI.Button(new Rect(100, 250, 100, 100), "ON/OFF"))        {            if (_webCamera != null && Plane != null)            {                if (_webCamera.isPlaying)                    StopCamera();                else                    PlayCamera();            }        }        if (GUI.Button(new Rect(100, 450, 100, 100), "Quit"))        {            Application.Quit();        }    }    public void PlayCamera()    {        Plane.GetComponent
().enabled = true; _webCamera.Play(); } public void StopCamera() { Plane.GetComponent
().enabled = false; _webCamera.Stop(); } public IEnumerator InitCameraCor() { yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; DeviceName = devices[0].name; _webCamera = new WebCamTexture(DeviceName, (int)CameraSize.x, (int)CameraSize.y, (int)CameraFPS); Plane.GetComponent
().material.mainTexture = _webCamera; Plane.transform.localScale = new Vector3(1, 1, 1); _webCamera.Play(); } }}

5.������������������

���������������������������������������������������������������������������������������������������������������������������������������������������ON/OFF ���������������������������������������������������������������������������������������������

������������������������������������������Unity3D������WebCamera������������������������������������������������������������������������������������������������������������������������������������������

上一篇:大佬教你用 Pyecharts 做出来的交互图表,领导说叼爆了!
下一篇:XML读取的几种方式

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月22日 19时00分14秒