#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");
}));
}
}
}