困った時の自分用メモ

読んだ本を考察してメモったり、自分でいじった物の感想をメモったりする場。週1更新を目指します。

Unityの話~画面のスクリーンショットを撮影する~

仕事で調査したので、メモ。

qiita.com

こちらの方の物を参考にして使わせてもらった。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScreenShotAndFileAccess : MonoBehaviour {
    
    [SerializeField]Image TestImage;

    private Texture2D TakeTexture;

    public void OnClickTakeScreenShotButton() {
        StartCoroutine(CoTakeScreenShot());
    }

    IEnumerator CoTakeScreenShot() {
        // http://robamemo.hatenablog.com/entry/2017/09/13/111843
        // レンダリングがすべて終わった後に再開という事らしい
        yield return new WaitForEndOfFrame();

        Debug.Log(string.Format("w/h:{0}/{1}", Screen.width, Screen.height));
        TakeTexture = new Texture2D(Screen.width, Screen.height);
        TakeTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        TakeTexture.Apply();
    }
    
    public void OnClickSetImageButton() {
        Sprite sprite = Sprite.Create(TakeTexture, new Rect(0, 0, Screen.width, Screen.height), Vector2.zero);
        TestImage.sprite = sprite;
    }
}

結果
f:id:mochimoffu:20190207192910p:plain

github.com

ScreenShotAndFileAccessシーンで確認可能。