Advertisement
Guest User

Untitled

a guest
May 28th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5.  
  6.  
  7. public class TakePhoto : MonoBehaviour {
  8.  
  9.     WebCamTexture _webCamTexture;
  10.     public string screenShotFileName;
  11.     private string _originPath;
  12.     private string _filePath;
  13.     private int screenShotCount = 0;
  14.     public GameObject button;
  15.     private byte[] bytesFile;
  16.     private Texture2D tex;
  17.     private bool shotTaken = false;
  18.     public SpriteRenderer _sr;
  19.    
  20.    
  21.  
  22.     void Start(){
  23.         _sr = gameObject.GetComponent<SpriteRenderer>() as SpriteRenderer;
  24.         _webCamTexture = new WebCamTexture ();
  25.         if (!Directory.Exists (Application.persistentDataPath + "/" + "Media")) {
  26.             Debug.Log ("Create Dir 1");
  27.             DirectoryInfo info = Directory.CreateDirectory (Application.persistentDataPath + "/" + "Media");
  28.         }
  29.         if (!Directory.Exists (Application.persistentDataPath + "/" + "Media/Pictures")) {
  30.             Debug.Log ("Create Dir 2");
  31.             DirectoryInfo info = Directory.CreateDirectory (Application.persistentDataPath + "/" + "Media/Pictures");
  32.         }
  33.     }
  34.     public void Timer(){
  35.         StartCoroutine (TakeSnapShot());
  36.     }
  37.     IEnumerator TakeSnapShot(){
  38.         screenShotCount++;
  39.         Debug.Log ("Button Deactivated");
  40.         button.SetActive (false);
  41.         //FileName Declareren
  42.         screenShotFileName = "Screenshot__" + screenShotCount + System.DateTime.Now.ToString ("__yyyy-MM-dd") + ".png";
  43.         //Screenshot maken
  44.         ScreenCapture.CaptureScreenshot(screenShotFileName);
  45.         yield return new WaitForSeconds (2);
  46.         shotTaken = true;
  47.         //Button weer naar active
  48.         button.SetActive (true);
  49.         SavePhoto();
  50.     }
  51.  
  52.     void SavePhoto(){
  53.         //Move To folder
  54.         if (shotTaken == true) {
  55.             _originPath = System.IO.Path.Combine(Application.persistentDataPath , screenShotFileName);
  56.             Debug.Log (_originPath + "Werkt");
  57.             _filePath =   Application.persistentDataPath + "/Media/Pictures/" + screenShotFileName;
  58.             if (System.IO.File.Exists (_originPath)) {
  59.                 System.IO.File.Move (_originPath, _filePath);
  60.                 Debug.Log ("Werkt wel, Foto opgeslagen onder" + _filePath);
  61.                 DisplayPicture ();
  62.  
  63.             } else {
  64.                 Debug.Log ("Werkt Niet");
  65.             }
  66.  
  67.         }
  68.  
  69.                
  70.  
  71.  
  72.         }
  73.     void DisplayPicture(){
  74.            
  75.                 //display picture
  76.                 Debug.Log("Foto Genomen");
  77.                 bytesFile = System.IO.File.ReadAllBytes(_filePath);
  78.                 Debug.Log (bytesFile);
  79.                  tex = new Texture2D(0, 0, TextureFormat.Alpha8, false);
  80.                  tex.LoadImage(bytesFile);
  81.                     tex.Apply ();
  82.                   if(tex != null){
  83.                     _sr.sprite = Sprite.Create(tex, new Rect(0,0,Screen.width,Screen.height), new Vector2(.5f, .5f));
  84.             }
  85.         }
  86.  
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement