Skip to content

Instantly share code, notes, and snippets.

@su10
Last active November 13, 2017 10:04
Show Gist options
  • Save su10/40d30c2bd91c705da116607c30ede653 to your computer and use it in GitHub Desktop.
Save su10/40d30c2bd91c705da116607c30ede653 to your computer and use it in GitHub Desktop.
【Unity】指定した解像度のスクリーンショットを実機なし・エディタだけで撮る方法【2017.1対応】 ref: https://qiita.com/su10/items/a8f3f825155835de3d2a
using System.Collections;
using UniRx;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Unityエディタ上からGameビューのスクリーンショットを撮るEditor拡張
/// </summary>
public class CaptureScreenshotFromEditor : Editor
{
/// <summary>
/// キャプチャを撮る
/// </summary>
/// <remarks>
/// Edit > CaptureScreenshot に追加。
/// HotKeyは Ctrl + Shift + F12。
/// </remarks>
[MenuItem("Edit/CaptureScreenshot #%F12")]
private static void CaptureScreenshot()
{
var fileName = System.DateTime.Now.ToString("スクリーンショット yyyy-MM-dd HH.mm.ss") + ".png";
Observable.FromCoroutine(() => CaptureCoroutine(fileName)).Subscribe();
}
private static IEnumerator CaptureCoroutine(string fileName)
{
var texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
yield return new WaitForEndOfFrame();
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
var bytes = texture.EncodeToPNG();
Destroy(texture);
System.IO.File.WriteAllBytes(fileName, bytes);
Debug.Log("ScreenShot: " + fileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment