gpt4 book ai didi

java - 为 unity 3D 创建原生 Flashlight Android Java 插件

转载 作者:行者123 更新时间:2023-12-02 03:07:18 27 4
gpt4 key购买 nike

我有一个问题,我不知道如何从 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# 函数:

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 访问堆栈跟踪。

如有任何想法,我们将不胜感激,谢谢您。

最佳答案

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

亲自调试 Android 插件是必须的。您可以使用 Unity/C# 端的 Debug.Log 和 Android/Java 端的 Log.V 来完成此操作。

使用 Android Studio 中的 Android Monitor 查看日志以及 Unity 中的异常日志。

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

您需要send instance of Context从 Unity 到 Android 插件。您也不必必须从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" />

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

关于java - 为 unity 3D 创建原生 Flashlight Android Java 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41570808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com