Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

AndroidJavaProxy

Namespace: UnityEngine

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.

// Opens an android date picker dialog and grabs the result using a callback.

// Dummy class to hold date - makes it easier to modify it in the callback class SelectedDate { static var date : Date = System.DateTime.Now; }

// The callback class class DateCallback extends AndroidJavaProxy { function DateCallback() { super("android.app.DatePickerDialog$OnDateSetListener"); } function onDateSet(view: AndroidJavaObject, year: int, monthOfYear: int, dayOfMonth: int){ SelectedDate.date = new Date(year, monthOfYear + 1, dayOfMonth); } }

function OnGUI () { if (GUI.Button (Rect (10,10,450,100), String.Format("{0:yyyy-MM-dd}", SelectedDate.date))) { var activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic.<AndroidJavaObject>("currentActivity"); activity.Call("runOnUiThread", new AndroidJavaRunnable(function(){ new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), SelectedDate.date.Year, SelectedDate.date.Month - 1, SelectedDate.date.Day).Call("show"); })); } }

using UnityEngine;
using System.Collections;

public class SelectedDate : MonoBehaviour {
    public static Date date = System.DateTime.Now;
}
public class DateCallback : MonoBehaviour {
    new ("android.app.DatePickerDialog$OnDateSetListener");
    void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth) {
        SelectedDate.date = new Date(year, monthOfYear + 1, dayOfMonth);
    }
}
public class ExampleClass : MonoBehaviour {
    void OnGUI() {
        if (GUI.Button(new Rect(10, 10, 450, 100), String.Format("{0:yyyy-MM-dd}", SelectedDate.date))) {
            AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            activity.Call("runOnUiThread", AndroidJavaRunnable(            new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), SelectedDate.date.Year, SelectedDate.date.Month - 1, SelectedDate.date.Day).Call("show");
));
        }
    }
}
import UnityEngine
import System.Collections

public class SelectedDate:

	public static date as Date = System.DateTime.Now

public class DateCallback(AndroidJavaProxy):

	public def constructor():
		super('android.app.DatePickerDialog$OnDateSetListener')

	def onDateSet(view as AndroidJavaObject, year as int, monthOfYear as int, dayOfMonth as int) as void:
		SelectedDate.date = Date(year, (monthOfYear + 1), dayOfMonth)

public class ExampleClass(MonoBehaviour):

	def OnGUI() as void:
		if GUI.Button(Rect(10, 10, 450, 100), String.Format('{0:yyyy-MM-dd}', SelectedDate.date)):
			activity as AndroidJavaObject = AndroidJavaClass('com.unity3d.player.UnityPlayer').GetStatic[of AndroidJavaObject]('currentActivity')
			activity.Call('runOnUiThread', AndroidJavaRunnable({ AndroidJavaObject('android.app.DatePickerDialog', activity, DateCallback(), SelectedDate.date.Year, (SelectedDate.date.Month - 1), SelectedDate.date.Day).Call('show') }))

Variables

javaInterfaceJava interface implemented by the proxy.

Constructors

AndroidJavaProxy

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.