unity3d调用jar类中的方法和属性

时间:2022-09-10 09:16:07

这里用的unity3d的版本是5.3.2

在eclipse里新建一个java工程,并导出成jar。

unity3d调用jar类中的方法和属性

package com.innyo.androidplugin;

public class apTest {

public apTest(String inString) {
tryProp = "propties is ok.<" + inString + ">";
}

public String tryProp = "public propties is return.";

public static String tryStaticProp = "static propties is return.";

public String TryPublic() {
return "public method run ok.";
}

public String TryPublic(String inString) {
return "public method with prop run ok.<" + inString + ">";
}

public static String TryStaticPublic() {
return "static public method run ok.";
}

public static String TryStaticPublic(String inString) {
return "static public method whith prop run ok.<" + inString + ">";
}
}

导出为jar

unity3d调用jar类中的方法和属性

unity3d调用jar类中的方法和属性


新建unity3d工程,导入jar文件。

unity3d调用jar类中的方法和属性

新建一个场景,把内容输出到text上显示,

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class TryPlugin : MonoBehaviour
{

public Text t;

public void ToTry ()
{
try {
t.text = "start android plugins";

t.text = t.text + "\r\n";
AndroidJavaClass jc = new AndroidJavaClass ("com.innyo.androidplugin.apTest");
t.text = t.text + "AndroidJavaClass jc:" + jc.ToString ();

t.text = t.text + "\r\n";
t.text = t.text + "tryProp:" + jc.Get<string> ("tryProp");

t.text = t.text + "\r\n";
t.text = t.text + "tryStaticProp:" + jc.GetStatic<string> ("tryStaticProp");

t.text = t.text + "\r\n";
t.text = t.text + "TryPublic:" + jc.Call<string> ("TryPublic");

t.text = t.text + "\r\n";
t.text = t.text + "TryPublic:" + jc.Call<string> ("TryPublic", "unity3d input");

t.text = t.text + "\r\n";
t.text = t.text + "TryStaticPublic:" + jc.CallStatic<string> ("TryStaticPublic");

t.text = t.text + "\r\n";
t.text = t.text + "TryStaticPublic:" + jc.CallStatic<string> ("TryStaticPublic", "unity3d input");

t.text = t.text + "\r\n";
t.text = t.text + "----------------------------------------";

t.text = t.text + "\r\n";
AndroidJavaObject jo = new AndroidJavaObject("com.innyo.androidplugin.apTest","unity3d jo build");
t.text = t.text + "AndroidJavaObject jo:" + jo.ToString ();

t.text = t.text + "\r\n";
t.text = t.text + "tryProp:" + jo.Get<string> ("tryProp");

t.text = t.text + "\r\n";
t.text = t.text + "tryStaticProp:" + jo.GetStatic<string> ("tryStaticProp");

t.text = t.text + "\r\n";
t.text = t.text + "TryPublic:" + jo.Call<string> ("TryPublic");

t.text = t.text + "\r\n";
t.text = t.text + "TryPublic:" + jo.Call<string> ("TryPublic", "unity3d input");

t.text = t.text + "\r\n";
t.text = t.text + "TryStaticPublic:" + jo.CallStatic<string> ("TryStaticPublic");

t.text = t.text + "\r\n";
t.text = t.text + "TryStaticPublic:" + jo.CallStatic<string> ("TryStaticPublic", "unity3d input");

} catch (Exception ex) {
t.text = t.text + "\r\n";
t.text = t.text + ex.Message;
}
}
}

编译成安卓,然后运行,点击按钮,可以看到以下结果

unity3d调用jar类中的方法和属性


AndroidJavaClass只能调用静态方法,获取静态属性

AndroidJavaObject能调用公开方法和公开属性

另外,jar文件,不一定非要放在plugins/android目录下。

例子代码:http://download.csdn.net/detail/wuyt2008/9444745


=====================================


如果是调用jar类中的枚举,而且枚举必须不是类下面的,方法如下

java

package com.innyo.androidplugin;

public enum TryEnum {
red,green,yellow,blcak

}


unity3d

AndroidJavaObject redValue = 
new AndroidJavaClass("com.innyo.androidplugin.TryEnum").GetStatic<AndroidJavaObject>("red");



如果是调用jar类中的接口,实在是不会在java中写事件接口,只好拿别人的用。

unity3d

public class DateCallback : AndroidJavaProxy
{
public DateCallback ()
: base ("com.amap.api.location.AMapLocationListener")
{
}

void onLocationChanged (AndroidJavaObject amapLocation)
{

}
}

等效于java中写

public class Hight_Accuracy_Activity implements AMapLocationListener {

@Override
public void onLocationChanged(AMapLocation loc) {

}
}