Version: 2022.3

AndroidJavaProxy

class in UnityEngine

切换到手册

描述

此类可用于实现任何 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); } }

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

公共函数

equals与 java.lang.Object equals() 方法等效。
hashCode与 java.lang.Object hashCode() 方法等效。
InvokeCalled by the java vm whenever a method is invoked on the java proxy interface. You can override this to run special code on method invocation, or you can leave the implementation as is, and leave the default behavior which is to look for c# methods matching the signature of the java method.
toString与 java.lang.Object toString() 方法等效。