为统一3D创建原生Flashlight Android Java插件

时间:2017-01-10 14:09:01

标签: java c# android unity3d

我有一个问题,我不知道如何从Unity访问我的java插件。

这是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;
}
}

在这里我的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;
}

但是当我进入时失败了:

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

我不知道如何调试,从我的android访问堆栈跟踪。

任何想法都表示赞赏,谢谢你的提前。

1 个答案:

答案 0 :(得分:2)

  

而且我不知道如何调试,从我的访问堆栈跟踪   机器人。

自己调试Android插件是必须的。您可以使用 Unity / C#中的Debug.Log Android / Java 方面的Log.V来执行此操作。

使用 Android Studio 中的 Android Monitor 来查看Unity中的日志和异常日志。

  

我不知道如何从Unity

访问我的java插件

您需要从Unity到Android插件send instance of Context。您还 必须从UnityPlayerActivity延伸。这里完全没必要。

<强>权限

<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" />

<强>爪哇:

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");
}