在自定义Unity Android插件上调用非静态方法 [英] Call non-static methods on custom Unity Android Plugins
问题描述
我试图了解如何为Android构建自己的Android插件.
I'm trying to understand how to build my own Android plugins for Android.
我实现了在Java类(在AndroidStudio上创建的类)上调用静态方法的方法,但实际上我不能调用非静态方法.
I achieve to call static methods on my Java class (the one created on AndroidStudio), but I really CAN'T call non-static methods.
我检查了那些链接:
http://leoncvlt. com/blog/a-primer-on-android-plugin-development-in-unity/
https://answers.unity.com/questions/1327186/how-to-get-intent-data-for-unity-to-use.html
但是没有一个.
我正在尝试从Unity的一个按钮接听电话,
I'm trying to get the call from a button from Unity like:
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.Get<AndroidJavaObject>("currentActivity");
currentActivity.Call("SayHi");
我在Android上的活动如下:
And my activity on Android looks like:
public class MainActivity extends UnityPlayerActivity {
private static final String TAG = "LibraryTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Created!");
}
public void SayHi()
{
Log.d(TAG, "HI_");
}
}
我的ADB抛出此消息:
My ADB is throwing this message:
我也尝试过调用,而不是像UnityPlayer那样调用它:
I've also tried calling instead of UnityPlayer call it like:
AndroidJavaClass pluginClass = new AndroidJavaClass("com.example.eric.librarytest.MainActivity");
这是我的AndroidManifest.xml
This is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eric.librarytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="24"
android:targetSdkVersion="28" />
<application android:label="@string/app_name" >
<activity
android:name="com.example.eric.librarytest.MainActivity"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
但是对于非静态方法也不起作用,如果我做了pluginClass.CallStatic("")
,它仅对静态方法有效吗?
But doesn't work either for non-static methods, it works only for static methods if I do pluginClass.CallStatic("")
, any idea?
-
Taras Leskiv建议更改UnityPlayer.Get to UnityPlayer.GetStatic,但随后出现以下错误:
Taras Leskiv suggest to change UnityPlayer.Get to UnityPlayer.GetStatic, but then i get the follow error:
错误:java.lang.NoSuchMethodError:在类Ljava.lang.Object;中没有名称为'SayHi'signature ='()V'的非静态方法;
error: java.lang.NoSuchMethodError: no non-static method with name='SayHi' signature='()V' in class Ljava.lang.Object;
推荐答案
我的代码正确,问题出在项目结构上.
My code was correct, the problem was the Project structure.
我的项目就像:
Assets
├── Plugins
│ ├── classes.jar
并且应该像这样:
Assets
├── Plugins
│ ├── Android
│ │ ├── classes.jar
是不是很讨厌(对我而言不是)导致垃圾邮件错误是找不到签名的消息,所以该软件无法读取这些类.
Was obvius (not for me) cause the error spam the message that could not find the signature, so the software can't read those classes.
这篇关于在自定义Unity Android插件上调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!