We cnotinue from the previous article where we created a JAR file and placed it in Unity. The current setup includes a simple UI and a script attached to the UseJavaExample GameObject.
Below is the Unity API for calling Java code:
void Start()
{
// Calling static methods
AndroidJavaClass javaClass = new AndroidJavaClass("your.class.name");
javaClass.CallStatic("MethodName"); // void static method
javaClass.CallStatic<string>("MethodName"); // static method with return value
javaClass.CallStatic("MethodName", "parameter"); // void static method with parameter
javaClass.CallStatic<int>("MethodName", "parameter"); // static method with return and parameter
// Static fields
javaClass.GetStatic<string>("FieldName");
javaClass.SetStatic("FieldName", "newValue");
// Calling instance methods
AndroidJavaObject javaObject = new AndroidJavaObject("your.class.name");
javaObject.Call("MethodName"); // void instance method
javaObject.Call<string>("MethodName"); // instance method with return
javaObject.Call("MethodName", "parameter"); // void instance method with parameter
javaObject.Call<int>("MethodName", "parameter"); // instance method with return and parameter
// Instance fields
javaObject.Get<string>("FieldName");
javaObject.Set("FieldName", "newValue");
}
Now we implement the code in UseJavaCodeExample.cs. The script references buttons, input fields, and a text display.
using UnityEngine;
using UnityEngine.UI;
public class UseJavaCodeExample : MonoBehaviour
{
public Button[] buttons;
public InputField[] textField;
public Text infoText;
private AndroidJavaClass javaClass = null;
private AndroidJavaObject javaObject = null;
void Start()
{
javaClass = new AndroidJavaClass("com.godshadow.test.Test");
javaObject = new AndroidJavaObject("com.godshadow.test.Test");
SetupButtonListeners();
}
private void SetupButtonListeners()
{
buttons[0].onClick.AddListener(SetLogStatic);
buttons[1].onClick.AddListener(GetLogStatic);
buttons[2].onClick.AddListener(SetLogFieldStatic);
buttons[3].onClick.AddListener(GetLogFieldStatic);
buttons[4].onClick.AddListener(SetNameInstance);
buttons[5].onClick.AddListener(GetNameInstance);
buttons[6].onClick.AddListener(SetNameFieldInstance);
buttons[7].onClick.AddListener(GetNameFieldInstance);
}
private void SetLogStatic()
{
javaClass.CallStatic("SetLog", textField[0].text);
infoText.text = "SetLogs: " + textField[0].text;
}
private void GetLogStatic()
{
string result = javaClass.CallStatic<string>("GetLog");
infoText.text = "GetLogs: " + result;
}
private void SetLogFieldStatic()
{
javaClass.SetStatic("Logs", textField[0].text);
infoText.text = "SetFieldLogs: " + textField[0].text;
}
private void GetLogFieldStatic()
{
string value = javaClass.GetStatic<string>("Logs");
infoText.text = "GetFieldLogs: " + value;
}
private void SetNameInstance()
{
javaObject.Call("SetName", textField[1].text);
infoText.text = "SetName: " + textField[1].text;
}
private void GetNameInstance()
{
string name = javaObject.Call<string>("GetName");
infoText.text = "GetName: " + name;
}
private void SetNameFieldInstance()
{
javaObject.Set("Name", textField[1].text);
infoText.text = "SetFieldName: " + textField[1].text;
}
private void GetNameFieldInstance()
{
string name = javaObject.Get<string>("Name");
infoText.text = "GetFieldName: " + name;
}
}
Important: This will only work when the applicasion is built and run on an Android device or emulator, not in the Unity Editor.