Instantly share code, notes, and snippets.

Embed
What would you like to do?
これを適当なGameObjectに貼り付けることで、Shift+Sで通常撮影(透明度ナシ)、Shift+Gで透明度あり撮影を行うことができます。
using System;
using System.Collections;
using System.IO;
using UnityEngine;
namespace Negipoyoc.SS
{
public class ScreenShot : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.S) && Input.GetKey(KeyCode.LeftShift))
{
ScreenCapture.CaptureScreenshot(DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png");
}
if (Input.GetKeyDown(KeyCode.G) && Input.GetKey(KeyCode.LeftShift))
{
StartCoroutine(CaptureWithAlpha());
}
}
IEnumerator CaptureWithAlpha()
{
yield return new WaitForEndOfFrame();
var tex = ScreenCapture.CaptureScreenshotAsTexture();
var width = tex.width;
var height = tex.height;
var texAlpha = new Texture2D(width, height, TextureFormat.ARGB32, false);
// Read screen contents into the texture
texAlpha.ReadPixels(new Rect(0, 0, width, height), 0, 0);
texAlpha.Apply();
// Encode texture into PNG
var bytes = texAlpha.EncodeToPNG();
Destroy(tex);
File.WriteAllBytes(Application.streamingAssetsPath + "/" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png", bytes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment