publicclassAndroidTest : MonoBehaviour { public Text txtOld; public Text txtNew; public Text txtStaticOld; public Text txtStaticNew; public Text txtTestFun; public Text txtTestStaticFun;
voidStart() { using (AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { //当前这个JavaObj就代表MainActivity这个对象,我们可以通过它获取到其中的变量、方法 using (AndroidJavaObject javaObj = javaClass.GetStatic<AndroidJavaObject>("currentActivity")) { //成员变量的获取和改变 int i = javaObj.Get<int>("testI"); txtOld.text = i.ToString(); javaObj.Set<int>("testI", 11); i = javaObj.Get<int>("testI"); txtNew.text = i.ToString(); //静态成员变量的获取和改变 int staticI = javaObj.GetStatic<int>("testStaticI"); txtStaticOld.text = staticI.ToString(); javaObj.SetStatic<int>("testStaticI", 21); staticI = javaObj.GetStatic<int>("testStaticI"); txtStaticNew.text = staticI.ToString();