Unity 保存图片文理

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4.   
  5. public class SavedScreen : MonoBehaviour {  
  6.   
  7.       
  8.     void Start () {  
  9.         // 开启一个协程  
  10.         StartCoroutine (UploadPNG());  
  11.     }  
  12.       
  13.   
  14.     // 定义一个协程  
  15.     IEnumerator UploadPNG () {  
  16.           
  17.   
  18.         // 因为"WaitForEndOfFrame"在OnGUI之后执行  
  19.         // 所以我们只在渲染完成之后才读取屏幕上的画面  
  20.         yield return new WaitForEndOfFrame();  
  21.   
  22.   
  23.   
  24.         int width = Screen.width;  
  25.         int height = Screen.height;  
  26.         // 创建一个屏幕大小的纹理,RGB24 位格(24位格没有透明通道,32位的有)  
  27.         Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);  
  28.   
  29.         // 读取屏幕内容到我们自定义的纹理图片中  
  30.         tex.ReadPixels (new Rect(0, 0, width, height), 0, 0);  
  31.         // 保存前面对纹理的修改  
  32.         tex.Apply ();  
  33.   
  34.         // 编码纹理为PNG格式  
  35.         byte[] bytes = tex.EncodeToPNG();  
  36.         // 销毁吴永的图片纹理  
  37.         Destroy (tex);  
  38.   
  39.         // 将字节保存成图片,这个路径只能在PC端对图片进行读写操作  
  40.         File.WriteAllBytes(Application.dataPath + "/onPcSavedScreen.png", bytes);  
  41.         // 这个路径会将图片保存到手机的沙盒中,这样就可以在手机上对其进行读写操作了  
  42.         File.WriteAllBytes(Application.persistentDataPath + "/onMobileSavedScreen.png", bytes);  
  43.   
  44.     }  
  45. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值