当前位置:网站首页>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);
}
}
边栏推荐
- Can GPU acceleration pytorch work?
- Sequence table - find main element
- antdPro - ProTable 实现两个选择框联动效果
- Paper reading and sharing
- Self use notes for problem brushing learning
- Undirected graph -- computing the degree of a node in compressed storage
- Static analysis of malicious code
- Opencv desaturation
- [JS component] customize the right-click menu
- 今日在家休息
猜你喜欢
Kotlin 协程的作用域构建器 coroutineScope与runBlocking 与supervisorScope,协程同步运行,协程挂掉的时候其他协程如何不被挂掉。
Arduino control soil moisture sensor
Three column simple Typecho theme lanstar/ Blue Star Typecho theme
Maybe we can figure out the essence of the Internet after the dust falls
磁盘分区方式对比(MBR与GPT)
Win10 home vs pro vs enterprise vs enterprise LTSC
[MRCTF2020]Ez_ bypass --BUUCTF
Download nail live playback through packet capturing
antdPro - ProTable 实现两个选择框联动效果
Mysql database password modification
随机推荐
Physical orbit simulation
Hard (magnetic) disk (I)
Android Weather
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
Self use notes for problem brushing learning
Canvas game lower level 100
[GYCTF2020]Ezsqli --BUUCTF
How many steps are appropriate for each cycle of deep learning?
Kotlin coroutine suspend function suspend keyword
Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
Programming training 1
[buglist] serial port programming does not read data
[JS component] custom paging
【SCA-CNN 解读】空间与通道注意力:Spatial and Channel-wise Attention
[003] embedded learning: creating project templates - using stm32cubemx
磁盘分区方式对比(MBR与GPT)
With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?
Dynamic planning - good article link
从ADK的WinPE自己手动构建自己的PE
Kotlin 协程挂起函数 suspend 关键字