示例代码如下(可以直接使用):
using UnityEngine;
using System.Collections;
using System;
public class AndroidProxyExample : MonoBehaviour
{
private static int dialogCb = 123;
class CycClickListener : AndroidJavaProxy
{
public CycClickListener() : base("android.content.DialogInterface$OnClickListener") { }
void onClick(AndroidJavaObject dialogInterface, int arg1)
{
Debug.Log("ClickListener ---- arg1:"+arg1);
dialogCb = arg1;
}
}
void OnGUI ()
{
if (GUI.Button(new Rect (15, 15, 450, 75), "cd:"+dialogCb.ToString()))
{
AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity");
activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
AndroidJavaObject builder = new AndroidJavaObject("android.app.AlertDialog$Builder", activity);
builder.Call("setTitle", "cyc title");
builder.Call("setMessage", "cyc this is test dialog msg");
builder.Call("setPositiveButton", "sure:)", new CycClickListener());
AndroidJavaObject dialog = builder.Call("create");
dialog.Call("show");
}));
}
}
运行结果:
原文链接:http://blog.csdn.net/yanchezuo/article/details/52261944