我正在尝试为Android制作一个统一插件。当单击按钮时,插件将显示本机android对话框。但是当我点击按钮时,catlog中会出现错误,并且会在设备上发生。但
Jar文件由Eclipse IDE构成。 Android Jar文件在类中包含此函数:
public void OpenDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(UnityPlayer.currentActivity);
builder.setTitle("Alert Dialog");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
我想尝试从Unity C#Script调用此函数:
void OnGUI()
{
if (GUI.Button(new Rect(0, 200, 100, 100), "Call Add Method"))
{
AndroidJavaObject jo = new AndroidJavaObject("com.werplay.unityplugin.AndroidPlugin");
jo.Call("OpenDialog");
}
}
这个错误出现在日食的catlog中:
答案 0 :(得分:0)
试试这个,你必须在你的线程中运行它,
public void OpenDialog() {
runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(
UnityPlayer.currentActivity);
builder.setTitle("Alert Dialog");
builder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}