using UnityEngine; using System.Collections; public class CameraTest : MonoBehaviour { public WebCamTexture cameraTexture; public string cameraName = ""; private bool isPlay = false; public Texture2D t; // Use this for initialization void Start() { StartCoroutine(OpenCamera()); t = new Texture2D(320, 240, TextureFormat.BGRA32, false); t.anisoLevel = 9; t.wrapMode = TextureWrapMode.Repeat; t.filterMode = FilterMode.Trilinear; } // Update is called once per frame void Update() { } /// <summary> /// 获取权限打开摄像头 /// </summary> /// <returns></returns> IEnumerator OpenCamera() { yield return Application.RequestUserAuthorization(UserAuthorization.WebCam); if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { WebCamDevice[] devices = WebCamTexture.devices; cameraName = devices[0].name; cameraTexture = new WebCamTexture(cameraName, 320, 240, 15); cameraTexture.Play(); isPlay = true; } } void OnGUI() { if (isPlay) { GUI.DrawTexture(new Rect(0, 0, 320, 240), cameraTexture, ScaleMode.ScaleAndCrop); Color32[] c = cameraTexture.GetPixels32(); t.SetPixels32(c); GUI.DrawTexture(new Rect(0, 240, 320, 240), t, ScaleMode.ScaleAndCrop); } } }