Advertisement
dnnkeeper

Unity Android Flashlight

Mar 7th, 2014
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. #if UNITY_ANDROID
  4. public class FL : MonoBehaviour {
  5.  
  6.     private bool Active;
  7.     private AndroidJavaObject camera;
  8.  
  9.     void FL_Start () {
  10.         AndroidJavaClass cameraClass = new AndroidJavaClass("android.hardware.Camera");
  11.         // This is an ugly hack to make Unity
  12.         // generate Camera permisions
  13.         WebCamDevice[] devices = WebCamTexture.devices;
  14.  
  15.         int camID = 0;
  16.         camera = cameraClass.CallStatic<AndroidJavaObject>("open", camID);
  17.        
  18.         // I'm pretty sure camera will never be null at this point
  19.         // It will either be a valid object or Camera.open would throw an exception
  20.         if (camera != null)
  21.         {
  22.             AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");
  23.             cameraParameters.Call("setFlashMode","torch");
  24.             camera.Call("setParameters",cameraParameters);
  25.             Active = true;
  26.         }
  27.         else
  28.         {
  29.             Debug.LogError("[CameraParametersAndroid] Camera not available");
  30.         }
  31.  
  32.     }
  33.  
  34.     void OnDestroy()
  35.     {
  36.         FL_Stop();
  37.     }
  38.  
  39.     void FL_Stop () {
  40.  
  41.         if (camera != null)
  42.         {
  43.             camera.Call("stopPreview");
  44.             camera.Call("release");
  45.             Active = false;
  46.         }
  47.         else
  48.         {
  49.             Debug.LogError("[CameraParametersAndroid] Camera not available");
  50.         }
  51.        
  52.     }
  53.  
  54.     // Update is called once per frame
  55.     void Update () {
  56.    
  57.     }
  58.  
  59.     void OnGUI()
  60.     {
  61.         GUILayout.BeginArea(new Rect(Screen.width*0.1f,Screen.height*0.1f,Screen.width*0.3f,Screen.height*0.1f) );
  62.         if (!Active)
  63.         {
  64.             if (GUILayout.Button("ENABLE FLASHLIGHT") )
  65.                 FL_Start();
  66.         }else{
  67.             if (GUILayout.Button("DISABLE FLASHLIGHT") )
  68.                 FL_Stop();
  69.         }
  70.         GUILayout.EndArea();
  71.     }
  72. }
  73. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement