当前位置:网站首页>Unity calls alertdialog

Unity calls alertdialog

2022-06-13 00:50:00 Small fish game development

How to operate see :https://blog.csdn.net/qq_17813937/article/details/83277457?spm=1001.2014.3001.5502

jar Package download :https://download.csdn.net/download/qq_17813937/82470778

effect

 Insert picture description here

Unity

The first is to write the properties of the pop-up class , Used to set pop-up information

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;
    }
}

Then write android proxy class , Android calls... Through a proxy unity The callback

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);
    }
}

Then pass the proxy class to android And open the pop-up window

public class AndroidHelper
{
    
    public static void ShowDialog(Dialog dialog)
    {
    
        if (dialog.buttons == null || dialog.buttons.Length == 0)
        {
    
            Debug.LogError(" There must be an end button to display the pop-up window ");
            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

Write the interface of the proxy class

package com.unity.mylibrary;

public interface AndroidDialog {
    
    public String GetTitle();
    public String GetMessage();
    public String[]GetButtons();
    public void OnButttonEvent(String title);
}

Show 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();
    }
}

Last in Activity Class added 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);
    }
}

test

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    
    void Start()
    {
    
        var dialog = new Dialog(" Warning "," Recharge me quickly ",new string[] {
     " well "});
        AndroidHelper.ShowDialog(dialog);
    }
}
原网站

版权声明
本文为[Small fish game development]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280557591412.html