Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Call a java function from eclipse ide in unity3d????

Discussion in 'Android' started by Milan_cool, Mar 11, 2011.

  1. Milan_cool

    Milan_cool

    Joined:
    Jan 24, 2011
    Posts:
    22
    Hii guys,
    .I dont want to write jni.I want to access a method in java ,i am referring below as example.
    AndroidJavaObject jo = new AndroidJavaObject("java.lang.String", "some_string"); // somewhat expensive
    int hash = jo.Call<int>("hashCode"); // first time - expensive
    int hash = jo.Call<int>("hashCode"); // second time
    i have a method getValue() in java .How to modify the above code to access getValue() method.
    Should i include jar file in the project for doing this? Please help me
     
  2. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    I don't know the AndroidJavaObject but equivalent to JNI I could imagine it's something like this:
    Code (csharp):
    1. AndroidJavaObject ajo = new AndroidJavaObject("YourJavaClassName", [is this parameter optional?]");
    2. ajo.invokeMethod("methodName", [parameter]);
     
  3. eriQue

    eriQue

    Unity Technologies

    Joined:
    May 25, 2010
    Posts:
    595
    If the getValue() method is part of some class you wrote yourself, then yes; you need to compile the .java code into a .class file, and compress it into a .jar file. Then copy it into the Assets/Plugins/Android/ folder. All this is covered by the Plugins documentation.

    From scripts you then create a AndroidJavaObject or AndroidJavaClass (depending on if your getValue is a simple static method or not), and call it with jo.Call<return_type>("getValue"); (or jc.CallStatic<return_type>("getValue"); if the method is declared static).