Unity存照片

来源:互联网 发布:台湾电视直播软件tv版 编辑:程序博客网 时间:2024/05/01 21:06

第一种:屏幕上所有内容都要保存,且没有格式限制

使用Application.CaptureScreenshot(string path)方法,其中path是图片存储的路径,保存的图片格式为png格式。

第二种:屏幕上所有内容都要保存,用jpg格式

代码:

IEnumerator Savephoto()
{
yield return new WaitForEndOfFrame ();
photoRT = new RenderTexture (1920, 1080, 24);
Camera.main.targetTexture = photoRT;
Camera.main.Render ();
RenderTexture.active = photoRT;
phototex = new Texture2D (1920, 1080, TextureFormat.RGB24, false);
phototex.ReadPixels (new Rect (0, 0, 1920, 1080), 0, 0);
RenderTexture.active = null;
Camera.main.targetTexture = null;
photopath = System.DateTime.Now.Month + " " + System.DateTime.Now.DayOfYear +" "+ System.DateTime.Now.Hour+ System.DateTime.Now.Minute+ System.DateTime.Now.Second+".jpg";
JPGEncoder encoder = new JPGEncoder (phototex, 50, Application.dataPath + "/Photo/" + photopath);
}

其中JPGEncoder百度之

第三种,保存摄像头拍摄到的画面(一部分或全部)

代码:

public IEnumerator Savephoto(){
yield return new WaitForEndOfFrame ();
Color[] pix = camtexture.GetPixels(120,0,544,544);

//其中camtexture是摄像头拍摄到的内容,WebCamTexture类型,此处参数的意思是从画面(120,0)处开始截取一个大小为544*544的区域,如果是保存拍摄的全部内容,参数设为(0,0,width,height)就行了
texture.SetPixels (pix);
texture.Apply ();
photoname = ((int)Time.time).ToString();
photourl = Application.dataPath + "/Photos/"+System.DateTime.Now.Date.Month.ToString()+" "+System.DateTime.Now.Date.Day.ToString()+" " + photoname+".jpg";
Debug.Log(photourl+"  photourl ");
JPGEncoder encoder = new JPGEncoder (texture, 50, photourl);
}

0 0
原创粉丝点击