Last active
August 4, 2019 20:19
-
-
Save keijiro/ee65e1d6edcb47912f34 to your computer and use it in GitHub Desktop.
Revisions
-
keijiro revised this gist
Dec 8, 2014 . 1 changed file with 0 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,13 +9,6 @@ public class PhotoTaker : MonoBehaviour public string temporaryFileName = "temp.png"; public string androidStoragePath = "/mnt/sdcard/TestCameraApp"; public void StartShooting() { StartCoroutine(ShootingCoroutine()); @@ -25,29 +18,11 @@ public void StartShooting() IEnumerator ShootingCoroutine() { TakeTemporaryScreenshot(); PlayShutterSound(); yield return StartCoroutine(WaitCaptureCompletion()); #if UNITY_EDITOR #elif UNITY_IOS AddToAlbum(TemporaryScreenshotPath); -
keijiro revised this gist
Dec 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ public class PhotoTaker : MonoBehaviour { // Screenshot file settings. public string temporaryFileName = "temp.png"; public string androidStoragePath = "/mnt/sdcard/TestCameraApp"; // Linked objects. public Behaviour[] behavioursToEnable; -
keijiro created this gist
Dec 8, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,153 @@ using UnityEngine; using System.Collections; using System.IO; using System.Runtime.InteropServices; public class PhotoTaker : MonoBehaviour { // Screenshot file settings. public string temporaryFileName = "temp.png"; public string androidStoragePath = "/mnt/sdcard/BeyooondAR"; // Linked objects. public Behaviour[] behavioursToEnable; public Behaviour[] behavioursToDisable; public GameObject[] objectsToEnable; public GameObject[] objectsToDisable; public Animator[] animatorsToStop; public void StartShooting() { StartCoroutine(ShootingCoroutine()); } // Main coroutine IEnumerator ShootingCoroutine() { foreach (var b in behavioursToEnable) b.enabled = true; foreach (var b in behavioursToDisable) b.enabled = false; foreach (var o in objectsToEnable) o.SetActive(true); foreach (var o in objectsToDisable) o.SetActive(false); foreach (var a in animatorsToStop) a.speed = 0; yield return null; TakeTemporaryScreenshot(); PlayShutterSound(); yield return StartCoroutine(WaitCaptureCompletion()); foreach (var b in behavioursToEnable) b.enabled = false; foreach (var b in behavioursToDisable) b.enabled = true; foreach (var o in objectsToEnable) o.SetActive(false); foreach (var o in objectsToDisable) o.SetActive(true); foreach (var a in animatorsToStop) a.speed = 1; #if UNITY_EDITOR #elif UNITY_IOS AddToAlbum(TemporaryScreenshotPath); #elif UNITY_ANDROID AddToAlbum(MoveToExternalStorage()); #endif } // Misc functions string Timestamp { get { return System.DateTime.Now.ToString("yyyy-MMdd-HHmm-ss"); } } string TemporaryScreenshotPath { #if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID) get { return Path.Combine(Application.persistentDataPath, temporaryFileName); } #else get { return temporaryFileName; } #endif } bool CheckFileInUse(string filePath) { FileStream stream = null; try { stream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { return true; } finally { if (stream != null) stream.Close(); } return false; } // Internal functions void TakeTemporaryScreenshot() { if (File.Exists(TemporaryScreenshotPath)) File.Delete(TemporaryScreenshotPath); Application.CaptureScreenshot(temporaryFileName); } void PlayShutterSound() { #if UNITY_EDITOR #elif UNITY_IOS PhotoTakerPlayShutterSound(); #elif UNITY_ANDROID var mediaActionSound = new AndroidJavaObject("android.media.MediaActionSound"); mediaActionSound.Call("play", mediaActionSound.GetStatic<int>("SHUTTER_CLICK")); #endif } IEnumerator WaitCaptureCompletion() { yield return new WaitForSeconds(0.1f); while (!File.Exists(TemporaryScreenshotPath)) yield return null; yield return new WaitForSeconds(0.1f); while (CheckFileInUse(TemporaryScreenshotPath)) yield return null; } string MoveToExternalStorage() { if (!Directory.Exists(androidStoragePath)) Directory.CreateDirectory(androidStoragePath); var destPath = Path.Combine(androidStoragePath, Timestamp + ".png"); File.Move(TemporaryScreenshotPath, destPath); return destPath; } void AddToAlbum(string filePath) { #if UNITY_EDITOR #elif UNITY_IOS PhotoTakerMoveImageToAlbum(filePath); #elif UNITY_ANDROID // string action = Intent.ACTION_MEDIA_SCANNER_SCAN_FILE AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); string action = intentClass.GetStatic<string>("ACTION_MEDIA_SCANNER_SCAN_FILE"); // Intent intentObject = new Intent(action); AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent", action); // Uri uriObject = Uri.parse("file:" + filePath); AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file:" + filePath); // intentObject.setData(uriObject); intentObject.Call<AndroidJavaObject>("setData", uriObject); // this.sendBroadcast(intentObject); AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity"); currentActivity.Call("sendBroadcast", intentObject); #endif } #if UNITY_IOS [DllImport("__Internal")] static extern void PhotoTakerPlayShutterSound(); [DllImport("__Internal")] static extern void PhotoTakerMoveImageToAlbum(string path); #endif }