gpt4 book ai didi

AndroidJAVAClass 无法执行我的非静态函数

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:48 26 4
gpt4 key购买 nike

我已经从我的统一代码中调用了 java 函数。我的 java 类中有静态和非静态类型的函数要访问第一个静态函数,我使用了:

AndroidJavaClass plugin = new AndroidJavaClass("com.android.test.TestActivity");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
plugin.CallStatic("InstantiateMe", qwe);

上面的代码运行良好,但在此之后,当我尝试执行我的非静态函数时,它实际上并没有执行..甚至没有给出任何异常或错误..

我调用非静态函数的代码

AndroidJavaClass plugin = new AndroidJavaClass("com.android.test.TestActivity");
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
plugin.Call("Enable", qwe);

帮我个忙。我卡在这里了。

最佳答案

将您的“Activity ”变量用于非静态调用:activity.Call("Enable", qwe);有静态和非静态函数调用的例子:

public class Caller {
private readonly AndroidJavaClass _ActivityClass;
private readonly AndroidJavaObject _ActivityObject;
private readonly AndroidJavaClass _MyActivityClass;

public Caller() {
_ActivityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_MyActivityClass = new AndroidJavaClass("com.example.u2aTest.MyMainActivity");

_ActivityObject = _ActivityClass.GetStatic("currentActivity");
// AndroidJavaObject my_activity_object = _MyActivityClass.GetStatic("currentActivity"); //doesn't work
}

public void MakeTestCalls() {
_ActivityObject.Call("testVoid"); // works fine
//_ActivityClass.CallStatic("testVoidStatic"); // doesn't works
_MyActivityClass.CallStatic("testVoidStatic"); // works fine
//_ActivityObject.CallStatic("testVoid"); // doesn't work on some divices

//public int testInt(int intValue)
int iresult = _ActivityObject.Call<int>("testInt", 999);

//public String testStringString(String paramValue)
String sresult = _ActivityObject.Call<String>("testStringString", "s2");

//public static int testIntStatic(int intValue)
iresult = _MyActivityClass.CallStatic<int>("testIntStatic", 999);

//public static String testStringStringStatic(String paramValue)
sresult =_MyActivityClass.CallStatic<String>("testStringStringStatic", "s1");
}
}

看看 example of Android plugin for Unity3D了解详情。

关于AndroidJAVAClass 无法执行我的非静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10701184/

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