Alexy Vercruysse
Alexy Vercruysse

Reputation: 269

Create a native Flashlight Android Java plugin for unity 3D

I have an issue, I don't know how to access to my java plugin from Unity.

Here is the Java Code :

package fr.vincentmazet.utilslibrary;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import com.unity3d.player.UnityPlayerActivity;


public class FlashLight extends UnityPlayerActivity {
private CameraManager camManager;
private Context context;

public void FlashLight(Context context){
    this.camManager = (CameraManager)    context.getSystemService(context.CAMERA_SERVICE);
    this.context = context;
}

public boolean enableFlash(){
        try {
            camManager.setTorchMode("0", true);
        } catch (CameraAccessException e) {
            e.printStackTrace();
    }
    return true;
}

public boolean stopFlash(){
        try {
            camManager.setTorchMode("0", false);
        } catch (CameraAccessException e) {
            e.printStackTrace();
    }
    return true;
}
}

And here my c# func :

public bool startFlash(){
    if (Application.platform == RuntimePlatform.Android) {
        using (var javaUnityPlayer = new AndroidJavaClass     ("com.unity3d.player.UnityPlayer")) {

            using (var currentActivity = javaUnityPlayer.GetStatic<AndroidJavaObject> ("currentActivity")) {

                using (var androidPlugin = new AndroidJavaObject ("fr.vincentmazet.utilslibrary.FlashLight", currentActivity)) {

                    return androidPlugin.Call<bool> ("enableFlash");
                }
            }
        }
    }
    return false;
}

But it failed when I enter in :

var androidPlugin = new AndroidJavaObject  ("fr.vincentmazet.utilslibrary.FlashLight", currentActivity)

And I don't know how to debug, access to the stack-trace from my android.

Any ideas is appreciated thank's you by advance.

Upvotes: 0

Views: 1879

Answers (1)

Programmer
Programmer

Reputation: 125275

And I don't know how to debug, access to the stack-trace from my android.

Debugging your Android plugin yourself is a must. You can do this with Debug.Log from Unity/C# and Log.V from Android/Java side.

Use Android Monitor from Android Studio to view the Logs and also Exception Log from Unity.

I don't know how to access to my java plugin from Unity

You need to send instance of Context from Unity to the Android Plugin. You also don't have to extend from UnityPlayerActivity. Totally unnecessary here.

Permission:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera.flash" android:required="false" />

Java:

public class FlashLight{
private CameraManager camManager;
static Context myContext;

// Called From C# to get the Context Instance
public static void receiveContextInstance(Context tempContext) {
        myContext = tempContext;

 this.camManager = (CameraManager)   myContext.getSystemService(context.CAMERA_SERVICE);
    }

public static boolean enableFlash(){
        try {
            camManager.setTorchMode("0", true);
        } catch (CameraAccessException e) {
            e.printStackTrace();
    }
    return true;
}

public static boolean stopFlash(){
        try {
            camManager.setTorchMode("0", false);
        } catch (CameraAccessException e) {
            e.printStackTrace();
    }
    return true;
}
}

C#:

AndroidJavaClass unityClass;
AndroidJavaObject unityActivity;
AndroidJavaObject unityContext;
AndroidJavaClass customClass;

void Start()
{
    //Replace with your full package name
    sendActivityReference("fr.vincentmazet.utilslibrary.FlashLight");

    //Enable Flashlight
    enableFlash();

   //Disable Flashlight
   //stopFlash();
}

void sendActivityReference(string packageName)
{
    unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

    customClass = new AndroidJavaClass(packageName);
    customClass.CallStatic("receiveContextInstance", unityContext);
}

bool enableFlash()
{
   return customClass.CallStatic<bool>("enableFlash");
}

bool stopFlash()
{
   return customClass.CallStatic<bool>("stopFlash");
}

Upvotes: 3

Related Questions