3

我从我的统一代码中调用了 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);

帮帮我,伙计。我被困在这里。

4

1 回答 1

6

将您的“活动”变量用于非静态调用: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");
  }
}

查看Unity3D 的 Android 插件示例以获取详细信息。

于 2012-11-13T02:59:27.400 回答