Skip to content

Instantly share code, notes, and snippets.

@negipoyoc
Last active October 1, 2023 08:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negipoyoc/5bf5db8e0187c167a823cb05ff1b2182 to your computer and use it in GitHub Desktop.
Save negipoyoc/5bf5db8e0187c167a823cb05ff1b2182 to your computer and use it in GitHub Desktop.
これを適当な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