当前位置:网站首页>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

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);
}
}
边栏推荐
- Arduino control tm1637 common positive four digit nixie tube
- Binary tree -- using hierarchical sequence and middle sequence to determine a tree
- Physical orbit simulation
- Canvas game 2048 free map size
- Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
- ROS2之OpenCV人脸识别foxy~galactic~humble
- Biological unlocking - Fingerprint entry process
- 深度学习模型剪枝
- 三角波与三角波卷积
- The scope builder coroutinescope, runblocking and supervisorscope of kotlin collaboration processes run synchronously. How can other collaboration processes not be suspended when the collaboration pro
猜你喜欢

Maybe we can figure out the essence of the Internet after the dust falls

Comparison of disk partition modes (MBR and GPT)

Kotlin coroutine suspend function suspend keyword

Download nail live playback through packet capturing

Four startup modes of kotlin collaboration

MySQL queries the quantity of each month and the year-on-year and month on month data of each month

ROS从入门到精通(零) 教程导读

Kotlin 协程挂起函数 suspend 关键字

STM32 USB Basics

gpu加速pytorch能用吗?
随机推荐
[JS component] previous queue prompt
什么是 dummy change?
Androi weather
antdPro - ProTable 实现两个选择框联动效果
Antdpro - protable realizes the linkage effect of two selection boxes
[JS component library] drag sorting component
How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
Androi天氣
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
Arduino control tm1637 common positive four digit nixie tube
AOF持久化
Breadth first search for node editor runtime traversal
Cve-2021-24078 vulnerability analysis
Sequence table - find main element
Set sail
Opencv desaturation
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
In / out / inout details of MySQL stored procedures
[ciscn2019 North China Day2 web1]hack world --buuctf