Unity3D随意截图并保存

http://blog.csdn.net/awnuxcvbn/article/details/9199245

效果

代码

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