Unity3D -- 自定义范围截屏
2015-11-21 11:55
531 查看
大家都知道unity有一个截屏方法,调用Application.CaptureScreenshot("Screenshot.png")方法就可以截取一张图保存到Application.persistentDataPath + "/Screenshot.png",
但是如果你只是想截取屏幕固定部分的内容怎么办呢,而且那个固定部分还是使用程序动态算出来的,这个时候使用全屏截取就会有错误了。
Unity是可以实现截取固定位置屏幕的。
首先在UI下面建立一个面板,
![](http://img.blog.csdn.net/20151121120130982?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
基于父对象中心对齐,然后长宽自由设定,
在父对象中声明一个public RectTransform BackGround 变量,然后将上面设置的面板关联到BackGround。
![](http://img.blog.csdn.net/20151121120431402?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
程序运行是ShareUI设置面板大小
m_Height = Screen.height;
m_Width = m_Height * m_Height / Screen.width;
BackGround.sizeDelta = new Vector2 (m_Width, m_Height);
这个可以随意设置。
然后开始截屏了。下面是代码:
Rect m_Rect;
void Start()
{
m_Height = Screen.height;
m_Width = m_Height * m_Height / Screen.width;
BackGround.sizeDelta = new Vector2 (m_Width, m_Height);
m_Rect= new Rect();
Vector3[] fourCornersArray = new Vector3[4];
BackGround.GetWorldCorners (fourCornersArray);
m_Rect.width = fourCornersArray [2].x - fourCornersArray [0].x;
m_Rect.height = fourCornersArray [2].y - fourCornersArray [0].y;
m_Scale = BackGround.sizeDelta.x / m_Rect.width;
m_Rect.width = BackGround.sizeDelta.x;
m_Rect.height = BackGround.sizeDelta.y;
m_Rect.x = Screen.width * 0.5f - m_Rect.width / 2;
m_Rect.y = Screen.height * 0.5f - m_Rect.height / 2;
}
IEnumerator Screenshot()
{
yield return new WaitForEndOfFrame();
BackGround.localScale = new Vector3 (m_Scale, m_Scale, m_Scale);
Texture2D screenShot = new Texture2D((int)m_Rect.width, (int)m_Rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(m_Rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.persistentDataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Invoke ("Share", 2f);
}
这样在需要截图的时候调用StartCoroutine (Screenshot());同样地就在Application.persistentDataPath + "/Screenshot.png"; 路径下保存了一份截屏文件。分享时将这种图片分享就可以了。
——Rocky
但是如果你只是想截取屏幕固定部分的内容怎么办呢,而且那个固定部分还是使用程序动态算出来的,这个时候使用全屏截取就会有错误了。
Unity是可以实现截取固定位置屏幕的。
首先在UI下面建立一个面板,
基于父对象中心对齐,然后长宽自由设定,
在父对象中声明一个public RectTransform BackGround 变量,然后将上面设置的面板关联到BackGround。
程序运行是ShareUI设置面板大小
m_Height = Screen.height;
m_Width = m_Height * m_Height / Screen.width;
BackGround.sizeDelta = new Vector2 (m_Width, m_Height);
这个可以随意设置。
然后开始截屏了。下面是代码:
Rect m_Rect;
void Start()
{
m_Height = Screen.height;
m_Width = m_Height * m_Height / Screen.width;
BackGround.sizeDelta = new Vector2 (m_Width, m_Height);
m_Rect= new Rect();
Vector3[] fourCornersArray = new Vector3[4];
BackGround.GetWorldCorners (fourCornersArray);
m_Rect.width = fourCornersArray [2].x - fourCornersArray [0].x;
m_Rect.height = fourCornersArray [2].y - fourCornersArray [0].y;
m_Scale = BackGround.sizeDelta.x / m_Rect.width;
m_Rect.width = BackGround.sizeDelta.x;
m_Rect.height = BackGround.sizeDelta.y;
m_Rect.x = Screen.width * 0.5f - m_Rect.width / 2;
m_Rect.y = Screen.height * 0.5f - m_Rect.height / 2;
}
IEnumerator Screenshot()
{
yield return new WaitForEndOfFrame();
BackGround.localScale = new Vector3 (m_Scale, m_Scale, m_Scale);
Texture2D screenShot = new Texture2D((int)m_Rect.width, (int)m_Rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(m_Rect, 0, 0);
screenShot.Apply();
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.persistentDataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Invoke ("Share", 2f);
}
这样在需要截图的时候调用StartCoroutine (Screenshot());同样地就在Application.persistentDataPath + "/Screenshot.png"; 路径下保存了一份截屏文件。分享时将这种图片分享就可以了。
——Rocky
相关文章推荐
- Shader 学习笔记 20151121
- Unity 延迟执行一段代码的较为优雅的方式
- Unity 延迟执行一段代码的较为优雅的方式
- Unity5 AssetBundle
- Unity各种路径获取
- Atmospheric Scattering in Unity5
- Atmospheric Scattering in Unity5
- Unity3D 关于刚体(RigitBody)与碰撞体(Collider)的探索
- 游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)
- Unity3D研究院之Time.timeScale、游戏暂停(七十四)
- 使用Unity3D的50个技巧:Unity3D最佳实践
- [UnityUI]使用UI框架
- Unity3d 调试
- Unity3D 控制角色移动,转向和自动跟随
- unity3d apk 重签名帮助提审
- Unity2D中使用鼠标拖动物体移动
- Unity3D中切换场景可能导致材质变暗的问题
- unity3d关于方法DontDestroyOnLoad的研究
- 高通AR-关于切换相机跟踪图片的控制
- 游戏蛮牛Unity 用户文档