Unity3D游戏开发之“屏幕截图”
2016-02-28 22:25
447 查看
在unity游戏开发中,可能会遇到在游戏中截屏的效果。这儿提供两种截屏方法。(方法二提供显示截图缩略图代码)
方法一:
在unity的API中,unity给我们提供了一个现成的API : Application.CaptureScreenshot(imagename),但是在我们使用这个API截图后的截图存放在哪儿呢?很多新朋友可能不是很清楚,当然不同的平台它的存放路径是有差别的。如果你想要你的游戏中显示你截图的缩略图,那么这种方法不是一个好方法,因为你要用 WWW去加载你刚才的截图,这会消耗你一部分的时间。
下面是各个平台的截图存放路径:
Application.CaptureScreenshot(screencapture.png)
if(Application.platform==RuntimePlatform.android || Application.platform==RuntimePlatform.IPhonePlayer)
imagePath=Application.persistentDataPath;
else if(Application.platform==RuntimePlatform.WindowsPlayer)
imagePath=Application.dataPath;
else if(Application.platform==RuntimePlatform.WindowsEditor)
{
imagePath=Application.dataPath;
imagePath= imagePath.Replace("/Assets",null);
}
imagePath = imagePath + "/screencapture.png";
方法二:
通过读取屏幕缓存然后转化为Png图片进行截图,并可直接使用png图片作为缩略图。(截图存储路径你可以自己设置)
IEnumerator GetCapture()
{
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width,height,TextureFormat.RGB24,false);
tex.ReadPixels(new Rect(0,0,width,height),0,0,true);
byte[] imagebytes = tex.EncodeToPNG();//转化为png图
tex.Compress(false);//对屏幕缓存进行压缩
image.mainTexture = tex;//对屏幕缓存进行显示(缩略图)
File.WriteAllBytes(Application.dataPath + "/screencapture.png",imagebytes);//存储png图
}
有兴趣的朋友可以试试这两种截图方法的性能和可用性。
方法一:
在unity的API中,unity给我们提供了一个现成的API : Application.CaptureScreenshot(imagename),但是在我们使用这个API截图后的截图存放在哪儿呢?很多新朋友可能不是很清楚,当然不同的平台它的存放路径是有差别的。如果你想要你的游戏中显示你截图的缩略图,那么这种方法不是一个好方法,因为你要用 WWW去加载你刚才的截图,这会消耗你一部分的时间。
下面是各个平台的截图存放路径:
Application.CaptureScreenshot(screencapture.png)
if(Application.platform==RuntimePlatform.android || Application.platform==RuntimePlatform.IPhonePlayer)
imagePath=Application.persistentDataPath;
else if(Application.platform==RuntimePlatform.WindowsPlayer)
imagePath=Application.dataPath;
else if(Application.platform==RuntimePlatform.WindowsEditor)
{
imagePath=Application.dataPath;
imagePath= imagePath.Replace("/Assets",null);
}
imagePath = imagePath + "/screencapture.png";
方法二:
通过读取屏幕缓存然后转化为Png图片进行截图,并可直接使用png图片作为缩略图。(截图存储路径你可以自己设置)
IEnumerator GetCapture()
{
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width,height,TextureFormat.RGB24,false);
tex.ReadPixels(new Rect(0,0,width,height),0,0,true);
byte[] imagebytes = tex.EncodeToPNG();//转化为png图
tex.Compress(false);//对屏幕缓存进行压缩
image.mainTexture = tex;//对屏幕缓存进行显示(缩略图)
File.WriteAllBytes(Application.dataPath + "/screencapture.png",imagebytes);//存储png图
}
有兴趣的朋友可以试试这两种截图方法的性能和可用性。
相关文章推荐
- Unity3D 5 官方教程:地形设置
- Unity之android推送
- 使用Unity3D引擎制作2D横版跑酷地图无限循环
- unity, public+[HideInInspector] vs private
- Unity用Shader实现波浪效果
- unity, 获取mesh名称
- Unity3D surf 函数,简单折射
- Unity3D 5 官方教程:SpeedTree/LOD Trees
- unity发布网页版(内嵌网页)
- unity2d角色防止二段跳解决方案
- Unity定时器
- Unity3d修炼之路:载入一个预制体,然后为该对象加入组件,然后查找对象,得到组件。
- 关于Unity碰撞和触发的总结
- 菜鸟在线Unity3d教程:如何获取摄像机图像?
- 菜鸟在线教你用Unity3D开发VR版的Hello World
- unity3d各种OpenFileDialog操作和Win MessageBox
- Unity IOS端 注意点
- 将NGUI的坐标转换Unity中的世界坐标
- 《Unity_API解析》 第四章 HideFlags类
- Unity3d本地存储