[转载]Unity3d三种截屏方法
2016-11-16 13:22
253 查看
转载地址:http://www.manew.com/blog-87668-3200.html
第一种截屏方法:
/// <summary>
///
使用Unity API中的CaptureScreenshot截屏方法
/// </summary>
public void CaptureScreen()
{
Application.CaptureScreenshot("截取全屏.png", 0);
}
第二种截屏方法:
/// <summary>
/// 自定义截图大小其中包含UI
/// </summary>
public Texture2D CaptureScreen(Rect rect)
{
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/ScreenShot.png";
System.IO.File.WriteAllBytes(filename, bytes);
return screenShot;
}
第三种截屏方法:
/// <summary>
/// 针对不同的相机进行截图,可以把UI和游戏分开
/// </summary>
/// <param name="c"></param>
/// <param name="r"></param>
/// <returns></returns>
public Texture2D CaptureScreen(Camera c, Rect r)
{
RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);
c.targetTexture = rt;
c.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(r, 0, 0);
screenShot.Apply();
c.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/ScreenShot.png";
System.IO.File.WriteAllBytes(filename, bytes);
return screenShot;
}
第一种截屏方法:
/// <summary>
///
使用Unity API中的CaptureScreenshot截屏方法
/// </summary>
public void CaptureScreen()
{
Application.CaptureScreenshot("截取全屏.png", 0);
}
第二种截屏方法:
/// <summary>
/// 自定义截图大小其中包含UI
/// </summary>
public Texture2D CaptureScreen(Rect rect)
{
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/ScreenShot.png";
System.IO.File.WriteAllBytes(filename, bytes);
return screenShot;
}
第三种截屏方法:
/// <summary>
/// 针对不同的相机进行截图,可以把UI和游戏分开
/// </summary>
/// <param name="c"></param>
/// <param name="r"></param>
/// <returns></returns>
public Texture2D CaptureScreen(Camera c, Rect r)
{
RenderTexture rt = new RenderTexture((int)r.width, (int)r.height, 0);
c.targetTexture = rt;
c.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)r.width, (int)r.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(r, 0, 0);
screenShot.Apply();
c.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/ScreenShot.png";
System.IO.File.WriteAllBytes(filename, bytes);
return screenShot;
}
相关文章推荐
- unity3d 游戏对象消失三种方法的区别(enabled/Destroy/active)
- VC++ 6.0下编写COM客户程序的三种方法(转载)
- [转载]ASP.NET 2.0 中跨网页提交的三种方法
- 转载:Java 将一行字符串追加至文件末尾的三种方法
- (转载)Recyclerview | Intent与Bundle在传值上的区别 | 设置布局背景为白色的三种方法
- 多线程的三种实现方法{转载}
- 在JS方法中返回多个值的三种方法(转载)
- Unity中三种截屏方法
- 三种方法实现js跨域访问(转载)
- Unity三种截屏方法(非自带API)
- Unity3D中三种调用其他脚本函数的方法
- unity3d shader之Roberts,Sobel,Canny 三种边缘检测方法
- 转载-按列连接字符串的三种方法
- Javascript 创建对象的三种方法及比较【转载+整理】
- 【转载】Java多线程,判断其他线程是否结束的三种方法
- 【转载】三种证明欧拉恒等式的方法(3 methods of proving Euler's Formula )
- Javascript定义类(class)的三种方法--(转载)原作者阮一峰
- 二叉树的三种遍历方法(递归和非递归)(转载)
- asp.net三种重定向方法的总结 (转载)
- 二叉树的三种遍历方法(递归和非递归)(转载)