Skip to content

Instantly share code, notes, and snippets.

@skishida
Created March 31, 2019 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skishida/e8528141c5dbdb5d2426cad7cf134020 to your computer and use it in GitHub Desktop.
Save skishida/e8528141c5dbdb5d2426cad7cf134020 to your computer and use it in GitHub Desktop.
Take Screenshot with alpha in Unity Editor
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// mixed these codes
// https://qiita.com/Nekomasu/items/dcdf73f221fd64875ef0
// http://entitycrisis.blogspot.com/2017/02/take-unity-screenshot-with-alpha.html
// Usage:
// 1. Attatch this script to Camera object
// 2. Play Game
// 3. Press Q in Game scene
// 4. Click "Shot" Button on left upper corner
public class SimpleScreenshot : MonoBehaviour
{
//ファイル名の指定
[SerializeField,Tooltip("ファイル名の末尾に付く文字")]
private string _imageTitle = "img";
//保存先の指定 (末尾に / を付けてください)
[SerializeField,Tooltip("ファイルの保存先 末尾の/ を含めてください")]
private string _imagePath = "Assets/ScreenShots/";
//タイムスタンプの形式
[SerializeField]
private int _timeStampType = 0;
//撮影ボタンの表示切替
[SerializeField,Tooltip("trueならGUIの撮影ボタンを表示します")]
private bool _shotButtonActive = false;
void Update()
{
//「Q」でボタンの表示切替
if (Input.GetKeyDown(KeyCode.Q))
{
_shotButtonActive = !_shotButtonActive;
}
//「P」で撮影
if (Input.GetKeyDown(KeyCode.P))
{
StartCoroutine(imageShooting(_imagePath,_imageTitle));
}
}
//撮影ボタン設定
void OnGUI()
{
if (_shotButtonActive == false) { return; }
if (GUI.Button(new Rect(10, 10, 40, 20), "Shot"))
{
StartCoroutine(imageShooting(_imagePath,_imageTitle));
}
}
//撮影処理
//第一引数 ファイルパス / 第二引数 タイトル
private IEnumerator imageShooting(string path, string title)
{
imagePathCheck(path);
string name = getTimeStamp() + title + ".png";
//ScreenCapture.CaptureScreenshot(path + name);
var camera = GetComponent<Camera>();
int w = camera.pixelWidth;
int h = camera.pixelHeight;
var rt = new RenderTexture(w, h, 32);
camera.targetTexture = rt;
var screenShot = new Texture2D(w, h, TextureFormat.ARGB32, false);
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, w, h), 0, 0);
screenShot.Apply();
camera.targetTexture = null;
RenderTexture.active = null;
DestroyImmediate(rt);
File.WriteAllBytes(Path.Combine(path, name), screenShot.EncodeToPNG());
Debug.Log("Title: " + name);
Debug.Log("Directory: " + path);
AssetDatabase.Refresh();
yield break;
}
//ファイルパスの確認
private void imagePathCheck(string path)
{
if (Directory.Exists(path))
{
Debug.Log("The path exists");
}
else
{
//パスが存在しなければフォルダを作成
Directory.CreateDirectory(path);
Debug.Log("CreateFolder: " + path);
}
}
//タイムスタンプ
private string getTimeStamp()
{
string time;
//タイムスタンプの設定書き足せます
// 0 数字の連番 / 20180101125959
// 1 数字の連番(年無し) / 0101125959
// 2 年月日付き / 2018年01月01日12時59分59秒
// 3 年月日付き(年無し) / 01月01日12時59分59秒
switch (_timeStampType)
{
case 0:
time = DateTime.Now.ToString("yyyyMMddHHmmss");
return time;
break;
case 1:
time = DateTime.Now.ToString("MMddHHmmss");
return time;
break;
case 2:
time = DateTime.Now.ToString("yyyy年MM月dd日HH時mm分ss秒");
return time;
break;
case 3:
time = DateTime.Now.ToString("MM月dd日HH時mm分ss秒");
return time;
break;
default:
time = DateTime.Now.ToString("yyyyMMddHHmmss");
return time;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment