unity中实现截取全屏的功能
1、截屏方法如下: /// /// 读取指定相机渲染的像素—本次使用的 /// private void ScreenShot_ReadPixelsWithCamera(Camera _camera) { //对指定相机进行 RenderTexture RenderTexture renTex = new RenderTexture(Screen.width, Screen.height, 16); _camera.targetTexture = renTex; _camera.Render(); RenderTexture.active = renTex; //读取像素 Texture2D tex = new Texture2D(Screen.width, Screen.height); tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); tex.Apply(); PictureList.Add(tex);//这一步是我需要,存储了一下截屏的图片,可以省去 MyRectTransform.GetComponent().sprite = ToSprite(tex); //读取目标相机像素结束,渲染恢复原先的方式 _camera.targetTexture = null; RenderTexture.active = null; Destroy(renTex); } 2、 将texture转换为sprite /// /// 将texture转换为sprite /// /// /// public Sprite ToSprite(Texture2D self) { var rect = new Rect(0, 0, self.width, self.height); var pivot = Vector2.one * 0.5f; var newSprite = Sprite.Create(self, rect, pivot); return newSprite; } 3、调用截屏方法即可: private void JiepingButtonClick() { ScreenShot_ReadPixelsWithCamera(Camera.main); }
|