标签:bytes 创建 captures ica height 图片 unit filename image
方法一:
Application.CaptureScreenshot("Screenshot.png", 0);
方法二:
/// <summary>
/// Captures the screenshot2.
/// </summary>
/// <returns>The screenshot2.</returns>
/// <param name="rect">Rect.截图的区域,左下角为o点</param>
Texture2D CaptureScreenshot2(Rect rect)
{
// 先创建一个的空纹理,大小可根据实现需要来设置,Scene.width,Scene,height获取全屏
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);
UnityEngine.Debug.Log(string.Format("截屏了一张图片: {0}", filename));
// 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。
return screenShot;
}
方法三:
public void shotPic()
{
var cam = gameObject.GetComponent<Camera>();
var renderTexture = new RenderTexture(Screen.width, Screen.height, 24);
cam.targetTexture = renderTexture;
cam.Render();
// 激活这个renderTexture, 并从中中读取像素。
RenderTexture.active = renderTexture;
Texture2D screenshot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
screenshot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenshot.Apply();
// 重置相关参数,以使camera继续在屏幕上显示
cam.targetTexture = null;
RenderTexture.active = null;
//Encode screenshot to PNG
byte[] bytes = screenshot.EncodeToPNG();
UnityEngine.Object.Destroy(screenshot);
File.WriteAllBytes(imagesPath + setPicName("test") + ".png", bytes);
}
标签:bytes 创建 captures ica height 图片 unit filename image
原文地址:http://www.cnblogs.com/leeplogs/p/6758851.html