using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
using UnityEngine.UI;
public class LocalFileServer : MonoBehaviour
{
public RectTransform UIRect;
public string outputImagePath; // 输出硬件图片的路径
private void Start()
{
outputImagePath = Application.streamingAssetsPath + "/out.png";
ShootDouble();
}
public void ShootDouble()
{
IEnumerator coroutine = CaptureByUI(UIRect, outputImagePath);
StartCoroutine(coroutine);
}
/// <summary>
///
/// </summary>
/// <param name="UIRect"></param>
/// <param name="mFileName"></param>
/// <returns></returns>
public IEnumerator CaptureByUI(RectTransform UIRect, string mFileName)
{
//等待帧画面渲染结束
yield return new WaitForEndOfFrame();
int width = (int)(UIRect.rect.width);
int height = (int)(UIRect.rect.height);
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
//左下角为原点(0, 0)
float leftBtmX = UIRect.transform.position.x + UIRect.rect.xMin;
float leftBtmY = UIRect.transform.position.y + UIRect.rect.yMin;
//从屏幕读取像素, leftBtmX/leftBtnY 是读取的初始位置,width、height是读取像素的宽度和高度
tex.ReadPixels(new UnityEngine.Rect(leftBtmX, leftBtmY, width, height), 0, 0);
//执行读取操作
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
//保存
System.IO.File.WriteAllBytes(mFileName, bytes);
}
}
⭐Unity 选定区域截图
最新推荐文章于 2024-05-17 11:27:10 发布