unity之截屏功能


1.全屏截圖

  方法一:在unity的API中,unity給我們提供了一個現成的API  :  Application.CaptureScreenshot(imagename)。

但是這個API雖然簡單,在PC、mac運用沒有多大的影響,但是如果是在移動平台上使用的話就顯得相當的吃力,因為它會消耗我們很大的CUP,所以在移動端使用它截屏的時候會發現很長一段的卡頓現象。但是不得不說使用起來非常的方便,但是在我們使用這個API截圖后的截圖在不同的平台它的存放路徑是有差別的。

下面是各個平台的截圖存放路徑:   

 Application.CaptureScreenshot(screencapture.png)

            if(Application.platform==RuntimePlatform.Android || Application.platform==RuntimePlatform.IPhonePlayer)  

                 imagePath=Application.persistentDataPath;  

            else if(Application.platform==RuntimePlatform.WindowsPlayer)  

                 imagePath=Application.dataPath;  

           else if(Application.platform==RuntimePlatform.WindowsEditor)

            {  

                 imagePath=Application.dataPath;  

                 imagePath= imagePath.Replace("/Assets",null);  

             }   

            imagePath = imagePath + "/screencapture.png";
Screenshot

         方法二:如果你想要你的游戲中顯示你截圖的縮略圖,那么這種方法不是一個好方法,因為你要用 WWW去加載你剛才的截圖,這會消耗你一部分的時間。   

點擊鼠標左鍵,實現截圖,並且將所截圖片保存到本地Assets目錄下的StreamingAssets文件夾下面。 

using UnityEngine;
using System.Collections;

public class TakeScreenShot : MonoBehaviour 
{
    void Update () 
    {
        //點擊鼠標左鍵
        if (Input.GetMouseButtonDown (0)) 
        {
            //開啟協程方法
            StartCoroutine (MyCaptureScreen ());
        }
    }
    //我的截屏方法
    IEnumerator MyCaptureScreen()
    {
        //等待所有的攝像機和GUI被渲染完成。
        yield return new WaitForEndOfFrame ();
        //創建一個空紋理(圖片大小為屏幕的寬高)
        Texture2D tex = new Texture2D (Screen.width,Screen.height);
        //只能在幀渲染完畢之后調用(從屏幕左下角開始繪制,繪制大小為屏幕的寬高,寬高的偏移量都為0)
        tex.ReadPixels (new Rect (0,0,Screen.width,Screen.height),0,0);
        //圖片應用(此時圖片已經繪制完成)
        tex.Apply ();
        //將圖片裝換成jpg的二進制格式,保存在byte數組中(計算機是以二進制的方式存儲數據)
        byte[] result = tex.EncodeToJPG ();
        //文件保存,創建一個新文件,在其中寫入指定的字節數組(要寫入的文件的路徑,要寫入文件的字節。)
        System.IO.File.WriteAllBytes (Application.streamingAssetsPath+"/1.JPG",result);
    }
}
TakeScreenShot
2截取自己想要的部分
using UnityEngine;
using System.Collections;
using System.IO;//引入IO流
public class NewBehaviourScript : MonoBehaviour 
{
    //定義一個存儲截屏圖片的路徑
    string filePath;
    void Start () 
    {
        //圖片存儲在StreamingAssets文件夾內。
        filePath = Application.streamingAssetsPath + "/1.png";
    }
    Rect rect;
    //截屏開始的位置
    Vector3 s_pos;
    //截屏結束的位置
    Vector3 e_pos;
    //是否繪制
    bool isDraw;
    void  Update()
    {
        //按下鼠標左鍵時,記錄當前鼠標的位置為開始截屏時的位置
        if(Input.GetMouseButtonDown(0))
        {
            s_pos = Input.mousePosition;
        }
        //鼠標處於按下狀態時
        if(Input.GetMouseButton(0))
        {
            e_pos = Input.mousePosition;
             //可以開始繪制
            isDraw = true;
        }
        //抬起鼠標左鍵時,記錄當前鼠標的位置為結束截屏時的位置
        if(Input.GetMouseButtonUp(0))
        {
            //結束繪制
            isDraw = false;
            e_pos = Input.mousePosition;
            //獲取到截屏框起始點的位置,和寬高。
            rect = new Rect(Mathf.Min(s_pos.x,e_pos.x),Mathf.Min(s_pos.y,e_pos.y),Mathf.Abs(s_pos.x-e_pos.x),Mathf.Abs(s_pos.y - e_pos.y));
            //開啟繪制的協程方法
            StartCoroutine(Capsture(filePath, rect));
        }
     }
    IEnumerator Capsture(string filePath, Rect rect)
    {
        yield return new WaitForEndOfFrame();
        //創建紋理(紋理貼圖的大小和截屏的大小相同)
        Texture2D tex = new Texture2D((int)rect.width, (int)rect.height);
        //讀取像素點
        tex.ReadPixels(rect, 0, 0) ;
        //將像素點應用到紋理上,繪制圖片
        tex.Apply();
        //將圖片裝換成jpg的二進制格式,保存在byte數組中(計算機是以二進制的方式存儲數據)
        byte[] result =tex.EncodeToPNG();
        //文件夾(如果StreamAssets文件夾不存在,在Assets文件下創建該文件夾)
        if(!Directory.Exists(Application.streamingAssetsPath))
        {
             Directory.CreateDirectory(Application.streamingAssetsPath);
        }
        //將截屏圖片存儲到本地
        File.WriteAllBytes(filePath, result);
    }
    //在這里要用GL實現繪制截屏的矩形框
    //1.GL的回調函數
    //2.定義一個材質Material
    public Material lineMaterial;

    void OnPostRender() 
    {
        if(!isDraw) return;
        print (s_pos);

        Vector3 sPos = Camera.main.ScreenToWorldPoint(s_pos + new Vector3(0, 0, 10));
        Vector3 ePos = Camera.main.ScreenToWorldPoint(e_pos + new Vector3(0, 0, 10));

        print(string.Format("GL.....{0},  {1}", sPos, ePos));
        // Set your materials Done
        GL.PushMatrix();
        // yourMaterial.SetPass( );
        lineMaterial.SetPass(0);//告訴GL使用該材質繪制
        // Draw your stuff
        //始終在最前面繪制
        GL.invertCulling = true;
        GL.Begin(GL.LINES);//開始繪制

        //GL.Vertex(sPos);
        //GL.Vertex(ePos);
        //如果想要繪制,矩形,將下面代碼啟動
        GL.Vertex(sPos);
        GL.Vertex(new Vector3(ePos.x, sPos.y, 0));


        GL.Vertex(new Vector3(ePos.x, sPos.y, 0));
        GL.Vertex(ePos);

        GL.Vertex(ePos);
        GL.Vertex(new Vector3(sPos.x, ePos.y, 0));

        GL.Vertex(new Vector3(sPos.x, ePos.y, 0));
        GL.Vertex(sPos);
        GL.End();//結束繪制

        GL.PopMatrix();
    }
}
NewBehaviourScript

 點擊查看原文


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM