首先界面是这样的,很简易,脚本上挂了三个按钮
直接上代码
using UnityEngine;
using UnityEngine.UI;
public class WebCamera : MonoBehaviour
{
WebCamTexture camTexture;
private WebCamDevice[] devices;
/// <summary>
/// 摄像头设备名
/// </summary>
string deviceName;
/// <summary>
/// 摄像头显示状态
/// </summary>
bool cameraIsOpen;
public Button openCamera, closeCamera, screenShot;
private void Awake()
{
//初始化摄像头显示的图像的大小
camTexture = new WebCamTexture(deviceName, 800, 600, 60);
}
private void Start()
{
cameraIsOpen = false;
openCamera.onClick.AddListener(OpenWebCamDevice);
closeCamera.onClick.AddListener(CloseWebCamDevice);
screenShot.onClick.AddListener(() =>
{
// 将图像传给截图方法
if (camTexture.isPlaying)
GetComponent<ScreenShot>().SaveScreenShot(camTexture);
});
}
//通过GUI绘制摄像头要显示的窗口
private void OnGUI()
{
//首先根据摄像头展示的画面来判断摄像头是否存在
if (cameraIsOpen && camTexture)
{
//绘制画面(Screen.width / 2 - 150f, Screen.height / 2 - 290,这里是画面距离场景的高和宽的限制)
//800, 600是和camTexture的画面一样大的绘制窗口
GUI.DrawTexture(new Rect(0, 0, 800, 600), camTexture);
}
if (!cameraIsOpen && camTexture)//不显示画面(没写这个步骤之前有个坑)
{
GUI.DrawTexture(new Rect(Screen.width / 2 - 150f, Screen.height / 2 - 290, 0, 0), camTexture);
}
}
/// <summary>
/// 打开摄像头
/// </summary>
void OpenWebCamDevice()
{
//用户授权
if (Application.HasUserAuthorization(UserAuthorization.WebCam))
{
//显示画面的设备就是要打开的摄像头
devices = WebCamTexture.devices;
//获取到设备名称
deviceName = devices[0].name;
//开启摄像头
camTexture.Play();
cameraIsOpen = true;
}
}
/// <summary>
/// 关闭摄像头
/// </summary>
void CloseWebCamDevice()
{
if (cameraIsOpen == true && camTexture != null)
{
cameraIsOpen = false;
camTexture.Stop();
}
}
}
截图脚本
using System;
using System.IO;
using UnityEngine;
public class ScreenShot : MonoBehaviour
{
readonly string savePath = Application.streamingAssetsPath + "/my"; // 图片保存路径
public void SaveScreenShot(Texture texture)
{
Texture2D jietuTexture = TextureToTexture2D(texture);
Save(jietuTexture);
}
/// <summary>
/// 保存图片
/// </summary>
/// <param name="texture2D"></param>
void Save(Texture2D texture2D)
{
byte[] textureBytes = texture2D.EncodeToPNG();
string filename = string.Format("IMG_{0}{1}{2}_{3}{4}{5}.jpg", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
Debug.Log("保存路径=====" + savePath + "/" + filename);
File.WriteAllBytes(savePath + "/" + filename, textureBytes);
if (File.Exists(savePath + "/" + filename))
Debug.Log("找到照片");
else
Debug.Log("未找到");
}
/// <summary>
/// Texture转换成Texture2D
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
private Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}
}
效果如图
截图 上面那张图让我不小心删了…