Skip to content

Instantly share code, notes, and snippets.

@celechii
Last active December 3, 2020 23:30
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 celechii/dd9d37e13ba8b060a67a9bf4b22013d1 to your computer and use it in GitHub Desktop.
Save celechii/dd9d37e13ba8b060a67a9bf4b22013d1 to your computer and use it in GitHub Desktop.
a little screenshot tool! takes a screenshot n saves it as a png onto ur desktop :)
using System;
using System.Collections;
using UnityEngine;
public class ScreenshotTool : MonoBehaviour {
private static ScreenshotTool control;
private void Awake() {
control = this;
}
#if UNITY_EDITOR
[UnityEditor.MenuItem("Take Screenshot")]
#endif
public static void Capture() {
GameObject temp = new GameObject("*SNAP*", typeof(ScreenshotTool));
temp.GetComponent<ScreenshotTool>().StartCoroutine(control.DoCapture());
}
private IEnumerator DoCapture() {
yield return new WaitForEndOfFrame();
Texture2D screenshot = NoTransparency(ScreenCapture.CaptureScreenshotAsTexture(), Camera.main.backgroundColor);
byte[] bytes = screenshot.EncodeToPNG();
UnityEngine.Object.Destroy(screenshot);
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = $"{path}/{Application.productName} @ {System.DateTime.Now.ToString("yyyy/M/d h:m:stt")}.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log("screenshot taken! ;D");
Destroy(gameObject);
}
public static Texture2D NoTransparency(Texture2D texture, Color transparencyColour) {
Color col;
for (int x = 0; x < texture.width; x++) {
for (int y = 0; y < texture.height; y++) {
col = texture.GetPixel(x, y);
if (col.a != 1) {
col = Color.Lerp(transparencyColour, col, col.a);
col.a = 1;
texture.SetPixel(x, y, col);
}
}
}
texture.Apply();
return texture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment