Unity3D随意截图并保存

在Unity开发中,有时需要我们截图分享,虽然Unity提供了封装好的方法:Application.CaptureScreenshot("**.png");但本人认为很多时候并不能满足我们的需求,故此,提供一卷比此更高级的截图方法。。。

代码

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4.   
  5.   
  6. public class CropPicture : MonoBehaviour {  
  7.   
  8.     string localPath = "http://192.168.1.100:8080/picture/15.jpg";   
  9.     Texture2D image;  
  10.     Texture2D cutImage;  
  11.     WWW www;  
  12.     Rect rect;  
  13.     float time;  
  14.     Vector2 pos1;  
  15.     Vector2 pos2;  
  16.     // Use this for initialization  
  17.     void Start () {  
  18.   
  19.         StartCoroutine(LoadImage());  
  20.     }  
  21.       
  22.     // Update is called once per frame  
  23.     void Update () {  
  24.         //点击鼠标左键,记录第一个位置  
  25.         if (Input.GetMouseButtonDown(0))  
  26.         {  
  27.             pos1 = Input.mousePosition;  
  28.             time = Time.time;  
  29.             if (time > 1f)  
  30.             {  
  31.                   
  32.                 Debug.Log(pos1);     
  33.             }  
  34.         }  
  35.         //放开左键记录第二个位置  
  36.         if (Input.GetMouseButtonUp(0))  
  37.         {  
  38.             pos2 = Input.mousePosition;  
  39.             Debug.Log(pos2);  
  40.             StartCoroutine(CutImage());  
  41.             time = 0;  
  42.         }  
  43.     }  
  44.   
  45.     void OnGUI()  
  46.     {  
  47.         //当下载完成  
  48.         if (www.isDone)  
  49.         {  
  50.             GUI.DrawTexture(new Rect(0, 0, 600, 904), image);  
  51.         }  
  52.   
  53.         GUI.Button(new Rect(0, 0, 100, 50), "W" + Screen.width + "H" + Screen.height);  
  54.   
  55.         if (pos1 != null)  
  56.         {  
  57.             GUI.Button(new Rect(0, 50, 150, 50),pos1.ToString());  
  58.         }  
  59.   
  60.         if (pos2 != null)  
  61.         {  
  62.             GUI.Button(new Rect(0, 100, 150, 50), pos2.ToString());  
  63.         }  
  64.   
  65.         if (cutImage != null)  
  66.         {  
  67.             GUI.Button(new Rect(0, 150, 150, 50), "image W" + cutImage.width + "H" + cutImage.height);  
  68.         }  
  69.   
  70.         if (rect != null)  
  71.         {  
  72.             GUI.Button(new Rect(0, 200, 250, 50), rect.ToString());  
  73.         }  
  74.     }  
  75.   
  76.     //下载图片  
  77.     IEnumerator LoadImage()  
  78.     {  
  79.         www = new WWW(localPath );  
  80.   
  81.         yield return www;  
  82.         image = www.texture;  
  83.         if (www.error != null)  
  84.         {  
  85.             Debug.Log(www.error);  
  86.         }  
  87.     }  
  88.   
  89.     //截图  
  90.     IEnumerator CutImage()  
  91.     {  
  92.   
  93.          
  94.         //图片大小  
  95.         cutImage = new Texture2D((int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y), TextureFormat.RGB24, true);  
  96.   
  97.           
  98.         //坐标左下角为0  
  99.         rect = new Rect((int)pos1.x, Screen.height-(int)(Screen.height-pos2.y), (int)(pos2.x - pos1.x), (int)(pos1.y - pos2.y));  
  100.   
  101.   
  102.         cutImage.ReadPixels(rect,0,0, false);   yield return new WaitForEndOfFrame();          
  103.     cutImage.Apply();  
  104.         yield return cutImage;  
  105.         byte[] byt = cutImage.EncodeToPNG();  
  106.         //保存截图  
  107.         File.WriteAllBytes(Application.dataPath + "CutImage.png", byt);    
  108.           
  109.     }  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值