该类可以用来实现任何 java 接口。代理对象上与接口匹配的任何 java vm 方法调用都会自动传递到 c# 中的实现。
注意:此 API 可用于自定义线程,但要求先将该线程附加到 JVM,请参阅 AndroidJNI.AttachCurrentThread。
// Opens an android date picker dialog and grabs the result using a callback. using UnityEngine; using System;
class ExampleClass : MonoBehaviour { private static DateTime selectedDate = DateTime.Now;
class DateCallback : AndroidJavaProxy { public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener") {} void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth) { selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth); // If you have no longer any uses for the object, it must be disposed to prevent a leak view.Dispose(); } }
void OnGUI() { if (GUI.Button(new Rect(15, 15, 450, 75), string.Format("{0:yyyy-MM-dd}", selectedDate))) { var activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"); activity.Call("runOnUiThread", new AndroidJavaRunnable(() => { new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), selectedDate.Year, selectedDate.Month - 1, selectedDate.Day).Call("show"); })); } } }
javaInterface | 代理实现的 Java 接口。 |
AndroidJavaProxy |