如何操作见:https://blog.csdn.net/qq_17813937/article/details/83277457?spm=1001.2014.3001.5502
jar包下载:https://download.csdn.net/download/qq_17813937/82470778
效果
Unity
首先是编写弹窗类的属性,用于设置弹窗信息
using System;
public class Dialog
{
public string title;
public string msg;
public string[] buttons;
public Action<string> onEvent;
public Dialog() { }
public Dialog(string title, string msg)
{
this.title = title;
this.msg = msg;
}
public Dialog(string title, string msg, string[] buttons)
{
this.title = title;
this.msg = msg;
this.buttons = buttons;
}
}
接着编写android代理类,安卓通过代理调用unity的回调
using UnityEngine;
public class AndroidDialog: AndroidJavaProxy
{
Dialog dialog;
public AndroidDialog(Dialog dialog) : base("com.unity.mylibrary.AndroidDialog")
{
this.dialog = dialog;
}
public string GetTitle() { return dialog.title; }
public string GetMessage() { return dialog.msg; }
public string[] GetButtons() { return dialog.buttons; }
public void OnButttonEvent(string title)
{
dialog.onEvent?.Invoke(title);
}
}
然后传递代理类到android并打开弹窗
public class AndroidHelper
{
public static void ShowDialog(Dialog dialog)
{
if (dialog.buttons == null || dialog.buttons.Length == 0)
{
Debug.LogError("必须有结束按钮才可显示弹窗");
return;
}
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var current = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
var androidDialog = new AndroidDialog(dialog);
current.Call("AndroidDialog", androidDialog);
}
}
JAVA
编写代理类的接口
package com.unity.mylibrary;
public interface AndroidDialog {
public String GetTitle();
public String GetMessage();
public String[]GetButtons();
public void OnButttonEvent(String title);
}
显示AlertDialog
package com.unity.mylibrary;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class AndroidDialogHelper{
public static void Show(android.app.Activity context,AndroidDialog dialog){
String[] buttons = dialog.GetButtons();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setTitle(dialog.GetTitle());
builder.setMessage(dialog.GetMessage());
builder.setPositiveButton(buttons[0], new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.OnButttonEvent(buttons[0]);
}
});
if(buttons.length>1){
for(int i=1;i<buttons.length;i++)
{
builder.setNegativeButton(buttons[i], new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialog.OnButttonEvent(buttons[i]);
}
});
}
}
builder.create();
builder.show();
}
}
最后在Activity类添加上AndroidDialog
package com.unity.mylibrary;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;
public class AndroidActivity extends UnityPlayerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void AndroidDialog(AndroidDialog dialog)
{
AndroidDialogHelper.Show(this,dialog);
}
}
测试
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
var dialog = new Dialog("警告","快给我充值",new string[] { "好的"});
AndroidHelper.ShowDialog(dialog);
}
}
|