/// <summary>
/// 使用相机 读取RenderTexture渲染像素 可指定区域 物体层级 截屏的原点为左上角
/// </summary>
Texture2D CaptureCamera(Camera camera, Transform zhuaqu)
{
RectTransform rectTransform = zhuaqu.GetComponent<RectTransform>();
int width = (int)rectTransform.rect.width;
int height = (int)rectTransform.rect.height;
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture(Screen.width, Screen.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(width, height, TextureFormat.RGB24, false);
//转化为左上角的坐标
float X = zhuaqu.localPosition.x + rectTransform.rect.xMin + Screen.width / 2f;
float Y = -zhuaqu.localPosition.y + rectTransform.rect.yMin + Screen.height / 2f;
screenShot.ReadPixels(new Rect(X, Y, width, height), 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();
//string filename = Application.streamingAssetsPath + "/Screenshot.png";
//System.IO.File.WriteAllBytes(filename, bytes);
//Debug.Log(string.Format("截屏了一张照片: {0}", filename));
//显示
imageShow.sprite = Sprite.Create(screenShot, new Rect(0, 0, width, height), new Vector2 (0.5f,0.5f));
imageShow.rectTransform.sizeDelta = new Vector2(width, height);
return screenShot;
}
/// <summary>
/// 读取屏幕像素 可指定区域 截屏的原点为左下角
/// </summary>
IEnumerator 截屏()
{
//等待渲染完成
yield return new WaitForEndOfFrame();
RectTransform 目标 = imageZhuaqu.GetComponent<RectTransform>();
// 计算出这个物体在屏幕的宽度(因为画布是经过缩放的,所以用实际宽度* 画布的缩放率)
int 宽度 = (int)(目标.rect.width * canvas.scaleFactor);
int 高度 = (int)(目标.rect.height * canvas.scaleFactor);
Texture2D tex = new Texture2D(宽度, 高度, TextureFormat.RGB24, false);
//通过中心点的位置和.transform.position可以计算出UI左下角的位置来
//要抓屏幕的物体.transform.position-这个参数其实就是UI控件的锚点所在屏幕位置
//RectTransform.rect.xMin代表左下角相对中心的实际位置(空间坐标?)所以乘以缩放率可以得到左下角与中心位置的屏幕距离
//在UI控件的锚点的屏幕坐标上加上差距 即的到左下角的屏幕位置
float 左下角X = imageZhuaqu.transform.localPosition.x + 目标.rect.xMin * canvas.scaleFactor + Screen.width / 2f;
float 左下角Y = imageZhuaqu.transform.localPosition.y + 目标.rect.yMin * canvas.scaleFactor + Screen.height / 2f;
Debug.Log(左下角X);
Debug.Log(左下角Y);
Debug.Log(imageZhuaqu.transform.localPosition.x);
Debug.Log(imageZhuaqu.transform.localPosition.y);
Debug.Log(目标.rect.xMin);
Debug.Log(目标.rect.yMin);
//用新建立的Texture2D,读取屏幕像素。
tex.ReadPixels(new Rect(左下角X, 左下角Y, 宽度, 高度), 0, 0);
//执行读取操作
tex.Apply();
//后面就是将Texture2D转换为sprinte并显示出来
Sprite 生成的 = Sprite.Create(tex, new Rect(0, 0, 宽度, 高度), Vector2.zero);
imageShow.sprite = 生成的;
imageShow.rectTransform.sizeDelta = new Vector2(宽度, 高度);
//也可以保存成PNG文件,今后使用
//byte[] 数据 = tex.EncodeToPNG();
//System.IO.File.WriteAllBytes("文件路径和名字", 数据);
}
-
最新
最热
- Unity3D打包发布后无法连接数据库问题 - 2,116 浏览
- WordPress新用户注册时提示“您的密码重设链接无效” - 1,868 浏览
- Unity资源热更新流程 - 1,707 浏览
2023年2月 一 二 三 四 五 六 日 « 5月 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 文章归档
帮助文档
联系我
功能