Last active
October 1, 2023 08:44
-
-
Save negipoyoc/5bf5db8e0187c167a823cb05ff1b2182 to your computer and use it in GitHub Desktop.
これを適当なGameObjectに貼り付けることで、Shift+Sで通常撮影(透明度ナシ)、Shift+Gで透明度あり撮影を行うことができます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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