C#调用JAVA(二)调用方法
上期我们创建了jar包并放到了unity中,那么我们继续
如果您还没有看上一期请先看上一期,这是链接
C#调用JAVA(一)制作jar包 - 执著 - 博客园 (cnblogs.com)
接着我们在unity中创建了简单的UI,并且创建了一个脚本
脚本挂在UseJavaExample 游戏对象上
在这里,我们先看一下Unity API
void Start()
{
//调用静态函数
AndroidJavaClass javaClass_1 = new AndroidJavaClass("your class name");
javaClass_1.CallStatic("MethodName"); //加载静态空返回值函数
javaClass_1.CallStatic<string>("MethodName"); //加载静态有返回值函数
javaClass_1.CallStatic("MethodName", "Android Studio"); //加载静态空返回值函数,并为函数赋参数值
javaClass_1.CallStatic<int>("MethodName", "Android Studio"); //加载静态有返回值函数,并为函数赋参数值
//获取静态字段,设置静态字段
javaClass_1.GetStatic<string>("FieldName");
javaClass_1.SetStatic("FieldName", "FieldValue");
//调用非静态函数
AndroidJavaObject javaObject_1 = new AndroidJavaObject("your class name");
javaObject_1.Call("MethodName"); //加载非静态空返回值函数
javaObject_1.Call<string>("MethodName"); //加载非静态有返回值函数
javaObject_1.Call("MethodName", "Android Studio"); //加载非静态空返回值函数,并为函数赋参数值
javaObject_1.Call<int>("MethodName", "Android Studio"); //加载非静态有返回值函数,并为函数赋参数值
//获取非静态字段,设置非静态字段
javaObject_1.Get<string>("FieldName");
javaObject_1.Set("FieldName", "FieldValue");
}
进入脚本写相应代码
脚本字段如下
UseJavaCodeExample.cs代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UseJavaCodeExample : MonoBehaviour
{
public Button[] buttons;
public InputField[] textField;
public Text infoText;
AndroidJavaClass javaClass = null;
AndroidJavaObject javaObject = null;
// Start is called before the first frame update
void Start()
{
javaClass = new AndroidJavaClass("com.godshadow.test.Test");
javaObject = new AndroidJavaObject("com.godshadow.test.Test");
SetUpButtonClickEvent();
}
private void SetUpButtonClickEvent()
{
buttons[0].onClick.AddListener(() => { SetLogsPressed(); });
buttons[1].onClick.AddListener(() => { GetLogsPressed(); });
buttons[2].onClick.AddListener(() => { SetFieldLogs(); });
buttons[3].onClick.AddListener(() => { GetFieldLogs(); });
buttons[4].onClick.AddListener(() => { SetNamePressed(); });
buttons[5].onClick.AddListener(() => { GetNamePressed(); });
buttons[6].onClick.AddListener(() => { SetFieldName(); });
buttons[7].onClick.AddListener(() => { GetFieldName(); });
}
private void SetLogsPressed()
{
javaClass.CallStatic("SetLog", textField[0].text);
infoText.text = "SetLogs:" + textField[0].text;
}
private void GetLogsPressed() => infoText.text = "GetLogs:" + javaClass.CallStatic<string>("GetLog");
private void SetFieldLogs()
{
javaClass.SetStatic("Logs", textField[0].text);
infoText.text = "SetFieldLogs:" + textField[0].text;
}
private void GetFieldLogs() => infoText.text = "GetFieldLogs:" + javaClass.GetStatic<string>("Logs");
private void SetNamePressed()
{
javaObject.Call("SetName", textField[1].text);
infoText.text = "SetName:" + textField[1].text;
}
private void GetNamePressed() => infoText.text = "GetName:" + javaObject.Call<string>("GetName");
private void SetFieldName()
{
javaObject.Set("Name", textField[1].text);
infoText.text = "SetFieldName:" + textField[1].text;
}
private void GetFieldName() => infoText.text = "GetFieldName:" + javaObject.Get<string>("Name");
}
注意:必须打包apk到Android或模拟器才有用