iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 4
0
自我挑戰組

深入淺出做中學Unity系列 第 6

Unity - 關於保留美好回憶這件小事(1)

每當好不容易完成一件事情,總會想保留一些圖像、影像或文字紀錄下這難得的一刻,但就像保留圖像需要照相機,保留影像需要有錄影功能的設備,就算只是記錄文字還是需要紙跟筆,把Unity當成一個世界的話,想在這個世界裡記錄下美好回憶該怎麼做呢?

首先我們可以試著使用照相機去保留一些圖像 :

一、 局部截圖

public RectTransform UIRect;
    void Update () {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            string fileName = Application.dataPath + "/StreamingAssets/" + "123.png";
            IEnumerator coroutine = CaptureUI(UIRect, fileName);
            StartCoroutine(coroutine);
        }

    }
    public IEnumerator CaptureUI(RectTransform UIRect, string mFileName)
    {
        //等待幀畫面渲染結束
        yield return new WaitForEndOfFrame();

        int width = (int)(UIRect.rect.width );
        int height = (int)(UIRect.rect.height);

        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        //左下角爲原點(0, 0)
        float leftBtmX = UIRect.transform.position.x + UIRect.rect.xMin ;
        float leftBtmY = UIRect.transform.position.y + UIRect.rect.yMin ;

        //從屏幕讀取像素, leftBtmX/leftBtnY 是讀取的初始位置,width、height是讀取像素的寬度和高度
        tex.ReadPixels(new Rect(leftBtmX, leftBtmY, width, height), 0, 0);
        //執行讀取操作
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(mFileName, bytes);
    }

在Canvas下新建一個Image,截去這個Image的所在區域,按下空格即可截取Image所在的區域,保留精華的部位細細品嚐。

二、 全屏截圖

  1. unity內置了一種非常簡單的截圖方法,保留完整的曲線體態,赤裸地呈現在眼前。
public void CaptureScreenUnity(string fileName)
{
  UnityEngine.ScreenCapture.CaptureScreenshot(fileName);
}
  1. 利用局部截圖裏面的方法,設置一個覆蓋全屏的UI組件,也可實現全屏截圖。

  2. 用OnPostRender方法進行截圖, OnPostRender是Camera類下的方法,所以要想執行該方法,腳本必須掛載在相機下面,截取的畫面是相機畫面,如果Canvas的RenderMode為默認的ScreenSpace-Overlay,截圖是不帶UI元素的。

//該腳本必須附加在Camera相機組件所在物體對象上OnPostRender方法才會執行
//當camera完成場景的渲染時會被執行
    private void OnPostRender()
    {
        if (grab)
        {
            //創建一個設置好的屏幕寬度和高度
            //Screen.width 和 Screen.height 爲在game窗口設置的寬度和高度
            Texture2D texture = new Texture2D(Screen.width, Screen.height,        
            TextureFormat.RGB24, false);
            
            texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
            texture.Apply();
            byte[] bytes = texture.EncodeToPNG();
            //保存
            System.IO.File.WriteAllBytes(fileName, bytes);
            grab = false;
        }
        
    }

三、包含UI的截圖(利用RenderTexture截圖)

需要新建一個Canvas,RenderMode設置爲ScreenSpace-Camera,把需要出現在截圖當中的元素放在該Canvas下面,然後在新建一個專門進行截圖的Camera,設置該Camera爲Canvas的渲染相機RenderCamera,便可以一眼望盡所有美麗的人事物,裹著糖衣不再赤裸,將所有美好盡收眼底。

public void CaptureScreenByRT(Camera camera, string fileName)
    {
        Rect rect = new Rect(0, 0, 1920, 1080);
        //創建一個RenderTexture對象  
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        //臨時設置相關相機的targetTexture爲rt, 並手動渲染相關相機  
        camera.targetTexture = rt;
        camera.Render();
        //開啟這個rt, 並從中中讀取像素。  
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height,  
        TextureFormat.RGB24, false);
        //從RenderTexture.active中讀取像素 
        screenShot.ReadPixels(rect, 0, 0); 
        screenShot.Apply();
        //重置相關參數,以使用camera繼續在屏幕上顯示  
        camera.targetTexture = null;
        RenderTexture.active = null; 
        GameObject.Destroy(rt);
        //將這些紋理數據,成一個png圖片文件  
        byte[] bytes = screenShot.EncodeToPNG();
        System.IO.File.WriteAllBytes(fileName, bytes);
    }

如此一來就能將想保留的事物以圖像的方式留存下來了呢,是不是很簡單呀,下一篇將要講的是如何保留影像的方式,有興趣的話千萬別錯過,每週一到五中午12點,Sun師綜合台 - 關於保留美好回憶這件小事。


上一篇
Unity - 組合粒子效果
下一篇
Unity – 關於保留美好回憶這件小事(2)
系列文
深入淺出做中學Unity9
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言