很简单,对于类对象,要保证在Android不能为空
void Awake()
{
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
activity.Call("setListener", new ActivityListener());
int fieldValue = activity.Get<int>("fieldValue");
UnityEngine.Debug.LogError("Test GetField : " + fieldValue);
activity.Set<int>("fieldValue", 9999999);
fieldValue = activity.Get<int>("fieldValue");
UnityEngine.Debug.LogError("Test SetField : " + fieldValue);
var length = activity.Call<AndroidJavaObject[]>("Test");
UnityEngine.Debug.LogError("CallObjectArray : " + fieldValue);
UnityEngine.Debug.LogError("CallObjectArrayReturn Length: " + length);
activity.Call<AndroidJavaObject[]>("Test")[0].Call("GetTest");
UnityEngine.Debug.LogError("Array Then call deep method" );
int index = activity.Call<AndroidJavaObject[]>("Test")[0].Get<int>("index");
UnityEngine.Debug.LogError("Array Then call deep variables : " + index);
string []srs = activity.Call<string[]>("GetStrings");
UnityEngine.Debug.LogError("return string array : " + srs[0]);
UnityEngine.Debug.LogError("return string array : " + srs[1]);
byte[] bytes = activity.Call<byte[]>("GetBytes");
UnityEngine.Debug.LogError("bytes.Length : === " + bytes.Length);
}
对应的Android部分代码是:
public void setListener()
{
Log.v("Unity", "setListener()!------------");
}
public Player[] Test()
{
Log.v("Unity", "test!------------");
Player[] ps = new Player[2];
ps[0] = new Player();
ps[1] = new Player();
return ps;
}
public String[] GetStrings()
{
String[] sarray = new String[2];
sarray[0] = "hahahStr1";
sarray[1] = "getStr2";
return sarray;
}
public byte[] GetBytes()
{
return new byte[10];
}
public class Player {
public int index = 12321321;
public void GetTest()
{
Log.v("Unity", "setListener(1)!------------");
}
}