Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- #if UNITY_ANDROID
- public class FL : MonoBehaviour {
- private bool Active;
- private AndroidJavaObject camera;
- void FL_Start () {
- AndroidJavaClass cameraClass = new AndroidJavaClass("android.hardware.Camera");
- // This is an ugly hack to make Unity
- // generate Camera permisions
- WebCamDevice[] devices = WebCamTexture.devices;
- int camID = 0;
- camera = cameraClass.CallStatic<AndroidJavaObject>("open", camID);
- // I'm pretty sure camera will never be null at this point
- // It will either be a valid object or Camera.open would throw an exception
- if (camera != null)
- {
- AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");
- cameraParameters.Call("setFlashMode","torch");
- camera.Call("setParameters",cameraParameters);
- Active = true;
- }
- else
- {
- Debug.LogError("[CameraParametersAndroid] Camera not available");
- }
- }
- void OnDestroy()
- {
- FL_Stop();
- }
- void FL_Stop () {
- if (camera != null)
- {
- camera.Call("stopPreview");
- camera.Call("release");
- Active = false;
- }
- else
- {
- Debug.LogError("[CameraParametersAndroid] Camera not available");
- }
- }
- // Update is called once per frame
- void Update () {
- }
- void OnGUI()
- {
- GUILayout.BeginArea(new Rect(Screen.width*0.1f,Screen.height*0.1f,Screen.width*0.3f,Screen.height*0.1f) );
- if (!Active)
- {
- if (GUILayout.Button("ENABLE FLASHLIGHT") )
- FL_Start();
- }else{
- if (GUILayout.Button("DISABLE FLASHLIGHT") )
- FL_Stop();
- }
- GUILayout.EndArea();
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement