Android Ultimate Plugin – Flashlight Tutorial

Hi Every one, today we will show you on how to use Flashlight using Android Ultimate Plugin for Unity3d.

First you need to make sure that you have an Android Manifest file on “Assets/Plugins/Android” directory.

Next make sure you have the necessary permissions for Flashlight on your Android Manifest file.

<!--for flash light-->
   <uses-permission android:name="android.permission.FLASHLIGHT"/>       
   <uses-permission android:name="android.permission.CAMERA"/>
   <uses-feature android:name="android.hardware.camera"/>
   <uses-feature android:name="android.hardware.camera.autofocus" />
   <uses-feature android:name="android.hardware.camera.flash" />    
<!--for flash light-->

To see the Android Manifest file use on demo please visit this link

http://www.gigadrillgames.com/2015/08/04/android-ultimate-plugin-android-manifest-permission-101/

After checking that you have an Android Manifest file on the correct Directory (Assets/Plugins/Android) and you have the required Android Manifest Permissions, it’s time to code.

First we need to have the instance of Flashlight Plugin

private FlashlightPlugin flashLightPlugin = FlashlightPlugin.GetInstance();

And then we need to set Debug to zero to hide Debug Toast Message, 0 to hide and 1 to show.

flashLightPlugin.SetDebug(0);

To Turn on Flashlight we just need to call SetFlashlightOn() method

flashLightPlugin.SetFlashlightOn();

To Turn off Flashlight we just need to call SetFlashlightOff() method

flashLightPlugin.SetFlashlightOff();

If you are done using the Flashlight we need to release it

flashLightPlugin.ReleaseFlashlight();

without releasing it, other application that needs Flashlight or Camera will not work, when this happens you need to restart your Android Device to force release the camera Services on Android Device

For Reference here’s the actual code that we use on our Demo

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class FlashlightDemo : MonoBehaviour {

    private FlashlightPlugin flashLightPlugin;
    private bool isFlashlightOn = false;    
    public DemoController demoController;
    public Text flashlightButtonText;

    // Use this for initialization
    void Start (){        
        flashLightPlugin = FlashlightPlugin.GetInstance();
        flashLightPlugin.SetDebug(0);

        SetText();
    }

    private void OnDisable(){
        ReleaseFlashlight();
    }

    private void SetText(){
        if(!isFlashlightOn){
            flashlightButtonText.text = "ON";
        }else{
            flashlightButtonText.text = "OFF";
        }
    }

    public void flashlightToggle(){
        if(!isFlashlightOn){
            flashLightPlugin.SetFlashlightOn();
            isFlashlightOn = true;
        }else{
            flashLightPlugin.SetFlashlightOff();
            isFlashlightOn = false;
        }

        SetText();
    }

    private void ReleaseFlashlight(){
        flashLightPlugin.SetFlashlightOff();
        isFlashlightOn = false;
        flashLightPlugin.ReleaseFlashlight();

        SetText();

        Debug.Log("[FlashlightDemo] release flashlight");
    }

    private void OnApplicationPause(bool pauseStatus){
        if(pauseStatus){
            ReleaseFlashlight();
        }
    }
}

 

and that’s it Guys, see how short and easy to use the flashlight and we really hope that we help you on this one.

Thanks for reading and visiting our website

For Question and Answer Forum please visit this link, you can post your question here and wait for some one to answer it but in order to do that you need to register first.

http://www.gigadrillgames.com/questions/

For more questions or suggestions or any feedback just email us at gigadrillgames@gmail.com

2 thoughts on “Android Ultimate Plugin – Flashlight Tutorial

  • July 28, 2016 at 12:13 am
    Permalink

    Hi, I’m developing an Augmented Reality App using Unity + Vuforia. I have two scenes in my project, the main scene that contains the AR Camera (the camera that detect the image target and reproduces the augmented reality) and a menu scene. When I set the flashlight script in the scene that doesn’t contains the AR camera, the flashlight turn on without any problem, but when I use the script in a button on the scene that contains the AR Camera, the function just dont work, I click the button and nothing happens. Can you help me? And congratulations for the amazing asset that is the Android Ultimate Pluggin, I have this one and i’m loving!

    Reply
    • July 28, 2016 at 8:12 am
      Permalink

      Hi, maybe the is vuforia is accessing the device camera that’s why when you use the flashlight it’s not working because only one application can access camera. so when two application will access it the 1st one will work and the second application will not work because the 1st one is already using it. i hope this shed some light.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.