
本文共 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������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
