using UnityEngine; using System.Collections; using System.IO; using System; using com.rfilkov.kinect; namespace com.rfilkov.components { /// /// PhotoShooter provides public functions to take pictures of the currently rendered scene. /// public class PhotoShooter : MonoBehaviour { [Tooltip("Camera used to render the scene. If not set, the main camera will be used.")] public Camera foregroundCamera; [Tooltip("Array of sprite transforms that will be used for displaying the countdown until image shot.")] public Transform[] countdown; [Tooltip("UI-Text used to display information messages.")] public UnityEngine.UI.Text infoText; // reference to the currently executed routine private IEnumerator currentRoutine = null; /// /// Checks if the photo making is currently in progress. /// public bool IsInProgress() { return (currentRoutine != null); } /// /// Counts down (from 3 for instance), then takes a picture and opens it /// public void CountdownAndMakePhoto() { if (currentRoutine != null) return; currentRoutine = CoCountdownAndMakePhoto(); StartCoroutine(currentRoutine); } // counts down (from 3 for instance), then makes a photo and opens it private IEnumerator CoCountdownAndMakePhoto() { if (countdown != null && countdown.Length > 0) { for (int i = 0; i < countdown.Length; i++) { if (countdown[i]) countdown[i].gameObject.SetActive(true); yield return new WaitForSeconds(1f); if (countdown[i]) countdown[i].gameObject.SetActive(false); } } MakePhoto(); yield return null; currentRoutine = null; } /// /// Saves the screen image as png picture, and then opens the saved file. /// public void MakePhoto() { MakePhoto(true); } /// /// Saves the screen image as png picture, and optionally opens the saved file. /// /// The file name. /// If set to true opens the saved file. public string MakePhoto(bool openIt) { int resWidth = Screen.width; int resHeight = Screen.height; Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); // hide the info-text, if any if (infoText) { infoText.text = string.Empty; } if(!foregroundCamera) { foregroundCamera = Camera.main; } // render the camera if (foregroundCamera && foregroundCamera.enabled) { foregroundCamera.targetTexture = rt; foregroundCamera.Render(); foregroundCamera.targetTexture = null; } // get the screenshot RenderTexture prevActiveTex = RenderTexture.active; RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); // clean-up RenderTexture.active = prevActiveTex; Destroy(rt); byte[] btScreenShot = screenShot.EncodeToJPG(); Destroy(screenShot); // save the screenshot as jpeg file string sDirName = Application.persistentDataPath + "/Screenshots"; if (!Directory.Exists(sDirName)) Directory.CreateDirectory(sDirName); string sFileName = sDirName + "/" + string.Format("{0:F0}", Time.realtimeSinceStartup * 10f) + ".jpg"; File.WriteAllBytes(sFileName, btScreenShot); Debug.Log("Photo saved to: " + sFileName); if (infoText) { infoText.text = "Saved to: " + sFileName; } // open file if (openIt) { System.Diagnostics.Process.Start(sFileName); } return sFileName; } } }