Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

AndroidJavaProxy

class in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

This class can be used to implement any java interface. Any java vm method invocation matching the interface on the proxy object will automatically be passed to the c# implementation.

#pragma strict
// Opens an android date picker dialog and grabs the result using a callback.
private static var selectedDate: DateTime = DateTime.Now;
class DateCallback extends AndroidJavaProxy {
	DateCallback"android.app.DatePickerDialog$OnDateSetListener" {
	}
	function onDateSet(view: AndroidJavaObject, year: int, monthOfYear: int, dayOfMonth: int) {
		selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
	}
}
function OnGUI() {
	if (GUI.Button(new Rect(15, 15, 450, 75), String.Format("{0:yyyy-MM-dd}", selectedDate))) {
		var activity: AndroidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic.<AndroidJavaObject>("currentActivity");
		activity.Call("runOnUiThread", new AndroidJavaRunnable(function() {
			new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), selectedDate.Year, selectedDate.Month - 1, selectedDate.Day).Call("show");
		}
		));
	}
}
// 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))) { AndroidJavaClass 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"); })); } } }

Variables

javaInterfaceJava interface implemented by the proxy.

Constructors

AndroidJavaProxy

Public Functions

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 invokation, 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.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961