unity点击按钮截取图片保存本地

菜鸟教程,欢迎大家指教
啥也不说直接上代码
// 定义一个协程
IEnumerator UploadPNG()
{
// 因为"WaitForEndOfFrame"在OnGUI之后执行
// 所以我们只在渲染完成之后才读取屏幕上的画面
yield return new WaitForEndOfFrame();

int width = Screen.width;
int height = Screen.height;
// 创建一个屏幕大小的纹理,RGB24 位格(24位格没有透明通道,32位的有)
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// 读取屏幕内容到我们自定义的纹理图片中
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
// 保存前面对纹理的修改
tex.Apply();
// 编码纹理为PNG格式
byte[] bytes = tex.EncodeToPNG();
// 销毁吴永的图片纹理
Destroy(tex);
// 将字节保存成图片,这个路径只能在PC端对图片进行读写操作
string path = System.Environment.CurrentDirectory + “/TuPian” + “-”+ “.png”;
File.WriteAllBytes(path, bytes);
// 这个路径会将图片保存到手机的沙盒中,这样就可以在手机上对其进行读写操作了
// File.WriteAllBytes(Application.persistentDataPath + “/onMobileSavedScreen.png”, bytes);
}
public void JieTu()
{
StartCoroutine(UploadPNG());
}
然后在按钮上直接添加事件就好了

猜你喜欢

转载自blog.csdn.net/qq_43024669/article/details/83897243
今日推荐