美文网首页
unity写android插件时的回调接口处理AndroidJa

unity写android插件时的回调接口处理AndroidJa

作者: Walk_In_Jar | 来源:发表于2019-05-23 12:39 被阅读0次

    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.

    using UnityEngine;
    using System;
    
    class ExampleClass : [MonoBehaviour](https://docs.unity3d.com/ScriptReference/MonoBehaviour.html)
    {
        private static DateTime selectedDate = DateTime.Now;
    
        class DateCallback : [AndroidJavaProxy](https://docs.unity3d.com/ScriptReference/AndroidJavaProxy.html)
        {
            public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener") {}
            void onDateSet([AndroidJavaObject](https://docs.unity3d.com/ScriptReference/AndroidJavaObject.html) view, int year, int monthOfYear, int dayOfMonth)
            {
                selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
            }
        }
    
        void OnGUI()
        {
            if ([GUI.Button](https://docs.unity3d.com/ScriptReference/GUI.Button.html)(new [Rect](https://docs.unity3d.com/ScriptReference/Rect.html)(15, 15, 450, 75), string.Format("{0:yyyy-MM-dd}", selectedDate)))
            {
                [AndroidJavaClass](https://docs.unity3d.com/ScriptReference/AndroidJavaClass.html) activity = new [AndroidJavaClass](https://docs.unity3d.com/ScriptReference/AndroidJavaClass.html)("com.unity3d.player.UnityPlayer").GetStatic<[AndroidJavaClass](https://docs.unity3d.com/ScriptReference/AndroidJavaClass.html)>("currentActivity");
                activity.Call("runOnUiThread", new [AndroidJavaRunnable](https://docs.unity3d.com/ScriptReference/AndroidJavaRunnable.html)(() =>
                {
                    new [AndroidJavaObject](https://docs.unity3d.com/ScriptReference/AndroidJavaObject.html)("android.app.DatePickerDialog", activity, new DateCallback(), selectedDate.Year, selectedDate.Month - 1, selectedDate.Day).Call("show");
                }));
            }
        }
    }</pre>
    
    

    怎么将日期选择返回到一个text呢

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class GetTimeClass
    {
        private static DateTime curDate = DateTime.Now;
    
    
        public delegate void TimeSelected(string time);
        static  TimeSelected callback;
        class DateCallback : AndroidJavaProxy
        {
            public DateCallback() : base("android.app.DatePickerDialog$OnDateSetListener"){}
            void onDateSet(AndroidJavaObject view, int year, int monthOfYear, int dayOfMonth)
            {
                //curDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
    
                callback(year+"/"+(monthOfYear + 1) +"/"+ dayOfMonth);
                callback = null;
            }
        }
     
        public static void GetTime(TimeSelected cb)
        {
            callback = cb;
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    
            activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
            {
                new AndroidJavaObject("android.app.DatePickerDialog", activity, new DateCallback(), curDate.Year, curDate.Month - 1, curDate.Day).Call("show");
            }));
           
        }
       
    }
    
    

    调用

        GetTimeClass.GetTime((time) => {
    
                title.text = time;
            });
    

    相关文章

      网友评论

          本文标题:unity写android插件时的回调接口处理AndroidJa

          本文链接:https://www.haomeiwen.com/subject/wgyvzqtx.html