public void CaptureScreenshot1(){第二种:截全屏(如下图, 注:有ui):
Application.CaptureScreenshot ("Screenshot.png", 0);
}
CaptureScreenshot2( new Rect( Screen.width*0f, Screen.height*0f, Screen.width*1f, Screen.height*1f));
截中间4分之1(如下图):
CaptureScreenshot2( new Rect( Screen.width*0.25f, Screen.height*0.25f, Screen.width*0.5f, Screen.height*0.5f));
Texture2D CaptureScreenshot2(Rect rect)第三种:针对某一相机截图,不包含UI。
{
// 先创建一个的空纹理,大小可根据实现需要来设置
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
// 读取屏幕像素信息并存储为纹理数据,
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
// 然后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一张图片: {0}", filename));
// 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。
return screenShot;
}
Texture2D CaptureCamera3(Camera camera, Rect rect)接下来调用方法,绑定按钮事件即可。
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活这个rt, 并从中中读取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32,false);
screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Apply();
// 重置相关参数,以使用camera继续在屏幕上显示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
//保存在StreamingAssets文件夹下
string filename = Application.streamingAssetsPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一张照片: {0}", filename));
//System.IO.File.WriteAllBytes(string.Format("/mnt/sdcard/dcim/{0}.png", System.DateTime.Now.ToString("yyyyMMddHHmmss")), bytes);
return screenShot;
}
public Camera camera1;
public void OnScreenButtonClick(){
//第一种截图方法(截全屏)
//CaptureScreenshot1 ();
//第二种截图方法
//CaptureScreenshot2( new Rect( Screen.width*0f, Screen.height*0f, Screen.width*1f, Screen.height*1f));
//第三种截图方法(截某一相机的图)
CaptureCamera3 (camera1, new Rect (Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f));
}
ps.2017.6.26 之前写的貌似问题很多 更新一下
public void screenShot1(){
//获取系统时间并命名相片名
System.DateTime now = System.DateTime.Now;
string times = now.ToString ();
times = times.Trim ();
times = times.Replace ("/","-");
string filename = "Screenshot"+times+".png";
//判断是否为Android平台
if (Application.platform == RuntimePlatform.Android) {
//截取屏幕
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
//转为字节数组
byte[] bytes = texture.EncodeToPNG();
string destination = "/storage/emulated/0/DCIM/Burst";
//判断目录是否存在,不存在则会创建目录
if (!Directory.Exists (destination)) {
Directory.CreateDirectory (destination);
}
string Path_save = destination+"/" + filename;
//存图片
System.IO.File.WriteAllBytes(Path_save, bytes);
}
}