简体   繁体   English

为统一3D创建本机Flashlight Android Java插件

[英]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. 我有一个问题,我不知道如何从Unity访问我的java插件。

Here is the Java Code : 这是Java代码:

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 : 这是我的C#函数:

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. 而且我不知道如何调试,可以从我的android访问堆栈跟踪。

Any ideas is appreciated thank's you by advance. 任何想法表示赞赏,谢谢你提前。

And I don't know how to debug, access to the stack-trace from my android. 而且我不知道如何调试,可以从我的android访问堆栈跟踪。

Debugging your Android plugin yourself is a must. 必须自己调试Android插件。 You can do this with Debug.Log from Unity/C# and Log.V from Android/Java side. 你可以做到这一点Debug.LogUnity / C#Log.VAndroid / Java方面。

Use Android Monitor from Android Studio to view the Logs and also Exception Log from Unity. 使用Android Studio中的 Android Monitor查看来自Unity的日志以及异常日志。

I don't know how to access to my java plugin from Unity 我不知道如何从Unity访问我的Java插件

You need to send instance of Context from Unity to the Android Plugin. 您需要Context实例从Unity发送到Android插件。 You also don't have to extend from UnityPlayerActivity . 您也不必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: 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#: 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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM