1. 程式人生 > >[Unity基礎]三種截圖方法

[Unity基礎]三種截圖方法

原文連結:

http://blog.csdn.net/anyuanlzh/article/details/17008909

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CaptureScreen : MonoBehaviour {
    void Start()
    {
        //Capture(); 
        //StartCoroutine(Capture2(new Rect(0, 0, Screen.width, Screen.height)));
        //Capture3(Camera.main, new Rect(0, 0, Screen.width, Screen.height));

        Camera uiCamera = GameObject.Find("UICamera").GetComponent<Camera>();
        Capture3(uiCamera, new Rect(0, 0, Screen.width, Screen.height));
    }

    public void Capture()
    {
        //儲存在工程的根目錄下
        Application.CaptureScreenshot("Screenshot.png");
        Debug.Log("Capture");
    }

    // 讀取螢幕畫素
    public IEnumerator Capture2(Rect rect)
    {
        // 先建立一個的空紋理,大小可根據實現需要來設定  
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);

        // 讀取螢幕畫素資訊並存儲為紋理資料
        yield return new WaitForEndOfFrame();
        screenShot.ReadPixels(rect, 0, 0, false);
        screenShot.Apply();

        // 然後將這些紋理資料,成一個png圖片檔案  
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.dataPath + "/Screenshot2.png";
        System.IO.File.WriteAllBytes(filename, bytes);

        AssetDatabase.Refresh();
        Debug.Log(string.Format("截圖了一張圖片: {0}", filename));
    }

    // 讀取RenderTexture畫素
    Texture2D Capture3(Camera camera, Rect rect)
    {
        // 建立一個RenderTexture物件  
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        // 臨時設定相關相機的targetTexture為rt, 並手動渲染相關相機  
        camera.targetTexture = rt;
        camera.Render();
        //ps: --- 如果這樣加上第二個相機,可以實現只截圖某幾個指定的相機一起看到的影象。  
        //ps: camera2.targetTexture = rt;  
        //ps: camera2.Render();  
        //ps: -------------------------------------------------------------------  

        // 啟用這個rt, 並從中中讀取畫素。  
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);// 注:這個時候,它是從RenderTexture.active中讀取畫素  
        screenShot.Apply();

        // 重置相關引數,以使用camera繼續在螢幕上顯示  
        camera.targetTexture = null;
        //ps: camera2.targetTexture = null;  
        RenderTexture.active = null; // JC: added to avoid errors  
        GameObject.Destroy(rt);

        // 最後將這些紋理資料,成一個png圖片檔案  
        byte[] bytes = screenShot.EncodeToPNG();
        string filename = Application.dataPath + "/Screenshot3.png";
        System.IO.File.WriteAllBytes(filename, bytes);
        AssetDatabase.Refresh();
        Debug.Log(string.Format("截圖了一張照片: {0}", filename));

        return screenShot;
    } 
}


相關推薦

[Unity基礎]方法

原文連結: http://blog.csdn.net/anyuanlzh/article/details/17008909 using UnityEngine; using System.Collections; using UnityEditor; public cl

華為手機的5方法,你都知道嗎?

如今蘋果手機不再那麼受人追捧,越來越多人開始使用華為手機,伴隨著智慧手機的出現,越來越多的手機功能讓我們感慨科技帶給我們的便利,就從華為說起,光是一個截圖,就有5種方法,究竟是哪5種方法,讓小編帶大家看一下! 手機按鍵 方法:電源鍵+音量下鍵,最傳統的使用方法,知道的人也是最多的。 快捷

IOS開發-幾方法

1.        UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomScale);        [pageView.page.layer renderInContext:

Python寫自動化之一方法

在進行自動化測試時,很多時候需要通過圖片對比來判斷某個功能是否正常,通常情況下,使用 Python 的第三方包 Image 可以 實現擷取圖片的功能。然而,有時候有些圖片是flash或者其他, 使用 Image 擷取到的可能是一片空白,舉個輸入法的例子來說,輸入法的狀態列,

Unity 方法

設定 保存 設置 res ren hot tor false 分享圖片 調用Unity API ScreenCapture.CaptureScreenshot("screen.png", 0); 截圖的圖片僅能保存為png格式,且第一個形參必須加.png

unity呼叫其他指令碼函式的方法

第一種,被呼叫指令碼函式為static型別,呼叫時直接用  指令碼名.函式名()第二種,GameObject.Find("指令碼所在的物體的名字").SendMessage("函式名"); //能呼叫public和private型別函式第三種,GameObject.Find(

Unity方法,在Unity中進行

今天我們討論下Unity3D的截圖方法,總共有三種方式。 1、利用Unity自帶的系統方式進行截圖: Application.CaptureScreenshot("Screenshot.png"); 2、利用Texture2D.ReadPixels()方法

通過fsharp 使用Enterprise Library Unity 3 - 攔截模式的探索

clean stc ane adding clas 是什麽 idc 內部函數 ash 這篇就三種攔截模式進行一下探索。 特性總結 類型 特點 其它 InterfaceInter

PHP中遍歷數組有常用的方法

log jin 存儲 方法 執行 ron () ech ++ 在PHP中操作最多的數據估計也就是數組了,有著效率高、速度快、存儲方便的特點。 PHP中遍歷數組有三種常用的方法:1、for循環,用法最為靈活,靈活到懷疑人生,但記住格式也非常簡單。2、foreach,是PHP為

linux的gzip、bzip2、xz壓縮使用方法工具介紹

linux bzip2 gzip xz 壓縮打包介紹:常用壓縮文件類型:1、windows:.rar/.zip/.7z2、linux:.zip/.gz/.bz2/.xz/.tar/.gz/.tar/.bz2/.tar/.xzgzip壓縮工具:1、實驗,cd到/tmp目錄下,使用mkdi創建一個

List集合遍歷方法

++ next string highlight gpo equal 方法 arraylist bject 一、通過叠代來遍歷List集合,可以刪除List集合中的元素。 import java.util.ArrayList; import java.util.Itera

談談vector容器的遍歷方法

封裝 font pri using != 說明 ace rac 三種 說明:本文僅供學習交流。轉載請標明出處。歡迎轉載! ? ? ? ? ?vector容器是最簡單的順序容器,其用法相似於數組。實際上vector的底層實現

spring屬性的註入方法

his ring prop pos string 名稱 BE per 構造方法 (1)使用set方法: public class Book { private String bookname;public void setBookname(String bookname)

公交車站撿垃圾之二叉樹的遍歷方法

info 表示 圖片 com 沒有 inf 不能 image 思考 # 二叉樹的遍歷 今天下午看了二叉樹的三種遍歷方式,雖然能寫出代碼,但是理解可能不太到位,感覺很容易忘,所以想到一個形象的方法,把每個節點當作公交車站,而訪問節點則是在這個公交車站撿垃圾,右子樹和左子樹則

java java中subString、split、stringTokenizer擷取字串方法的效能比較

面試的時候,string  基本上是必須問的知識   突然想起面試的時候曾經被人問過:都知道在大資料量情況下,使用String的split擷取字串效率很低,有想過用其他的方法替代嗎?用什麼替代?我當時的回答很斬釘截鐵:沒有。 google了一下,發現有2中替代方法,於

字典遍歷方法和排序方法

字典遍歷方法挺多,我總結了以下比較常見的三種: 1、用dict.keys()遍歷 用dict.keys()方法會返回一個型別為dict_keys的序列型別,裡面有字典的key,然後遍歷即可 2、有dict.values()遍歷 類似的,會返回字典的所有value值組成的一個序列,型別為d

方式(鄰接矩陣,鄰接表,鏈式前向星)

#include<cstdio> #include<vector> #include<cstring> #include<algorithm> #include <iostream> using namespace std; const i

Python視覺化中Matplotlib(4.設定樣式方法、設定座標刻度以及標籤、設定顯示出特殊字元)

1.三種設定方式   (1)向方法傳入關鍵字引數 上一節已經總結過,一直在使用   (2)對例項使用一系列的setter方法           具體的方法直接看程式碼  import matplotli

Java中 List的遍歷及遍歷方法

Java List遍歷方法 及其效率對比 package com.zbalpha.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List;

樹的存儲方法

int str color png pre bsp tree parent com 一、雙親存儲結構 typedef struct { int data; int parent; }PTree[max_size];//P表示parent圖像如下