Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Created January 29, 2015 08:00
Show Gist options
  • Save kirillrybin/8383471afce061ff35b0 to your computer and use it in GitHub Desktop.
Save kirillrybin/8383471afce061ff35b0 to your computer and use it in GitHub Desktop.
Unity3D/C# class that takes screenshot from SFCamera. Works with Scaleform Unity Plugin.
using Scaleform;
using UnityEngine;
[RequireComponent(typeof (SFCamera))]
public class SFCameraSnapshot : MonoBehaviour
{
private bool _takeShot;
private Rect _rect;
public void Shoot(Rect getSnapshotRect)
{
_rect = getSnapshotRect;
_takeShot = true;
}
private void OnPostRender()
{
if (_takeShot)
{
var resWidth = (int)_rect.width;
var resHeight = (int)_rect.height;
var screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
screenShot.ReadPixels(_rect, 0, 0);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
_takeShot = false;
}
}
public static string ScreenShotName(int width, int height)
{
return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
Application.dataPath,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment