Question by
Floorstreet · Jan 19, 2016 at 10:04 AM ·
androidoptimizationflashlight
(Newbie) Optimize this android LED-toggle?
Hello, hate to waste peoples time but I don't know how to move forward. About a week ago I started making my first project, which will be an app. Everything was going suprisingly well, until I got to this part. I managed after a few hours to come up with this code. A script that is assigned to one button as a toggle.
using UnityEngine;
using System.Collections;
public class FlashToggle : MonoBehaviour {
private bool Active;
private AndroidJavaObject camera1;
public void FL_Toggle()
{
if (Active == true)
{
camera1.Call("stopPreview");
camera1.Call("release");
Active = false;
}
else
{
AndroidJavaClass cameraClass = new AndroidJavaClass ("android.hardware.Camera");
WebCamDevice[] devices = WebCamTexture.devices;
camera1 = cameraClass.CallStatic<AndroidJavaObject> ("open", 0);
if (camera1 != null) {
AndroidJavaObject cameraParameters = camera1.Call<AndroidJavaObject> ("getParameters");
cameraParameters.Call ("setFlashMode", "torch");
camera1.Call ("setParameters", cameraParameters);
camera1.Call ("startPreview");
Active = true;
}
}
}
}
The code works, problem is it lags my phone for about 1.5 seconds until the LED(Front flashlight) turns off/on. Does anyone know how to make this smoother? Thanks in advance. Oh, and as I said I'm really new, 90% of times I figure things out... After some time, I'd appreciate if you could keep it idiot-friendly - IF possible :)
Comment