为Unity Mobile App显示Android Date Picker DIALOGUE

时间:2019-06-22 01:17:10

标签: c# android unity3d

如何在按下按钮时显示本机Android Date Picker对话框。我看了一下Unity Docs unity docs AndroidJavaProxy上的示例,但是它似乎只用日期填充了create按钮,并且没有像在页面上建议的那样显示Date Picker DIALOGUE。在Samsung Galaxy Note 8上进行测试。未提供任何代码,因为它与LINK完全相同。

2 个答案:

答案 0 :(得分:2)

using UnityEngine;
using TMPro;
using System;

public class SelectedDate
{
    public static DateTime date = DateTime.Now;
}
public class DateCallback : AndroidJavaProxy
{
    public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener") { }

    public void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth)
    {
        SelectedDate.date = new DateTime(year, monthOfYear + 1, dayOfMonth);
    }
}
public class DatePicker : MonoBehaviour
{
    public TMP_Text startingDateText;
    public TMP_Text goalDateText;
    private AndroidJavaObject activity;
    private DateTime newSelectedStartingDate;
    private DateTime newSelectedGoalDate;

    void PickDate()
    {
        new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), SelectedDate.date.Year, SelectedDate.date.Month - 1, SelectedDate.date.Day).Call("show");
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 450, 100), string.Format("ddd d MMMM, yyyy", SelectedDate.date)))
        {
            activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            activity.Call("runOnUiThread", new AndroidJavaRunnable(PickDate));
        }
    }
}

这似乎可行,但只能通过OnGUI正确执行,并在Android Date Picker Dialogue关闭后进行更新。在下次单击按钮之前,不会在Unity UI中创建按钮并在单击按钮时执行代码。

答案 1 :(得分:0)

问题在于“活动”类型,该类型必须为AndroidJavaObject而不是AndroidJavaClass

注意:GetStatic<T>(Object)方法也将AndroidJavaObject作为类型参数。

原文:

AndroidJavaClass activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaClass>("currentActivity");

已修复:

AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");