前言
Unity3D可以使用UGUI自己设计弹窗,也可以像Winform一样使用Windows系统本身的弹窗(跨平台请谨慎使用)。
原文:Unity调用Windows弹框、提示框(确认与否,中文)
其他:https://www.jianshu.com/p/5a930906df03
部分效果如下:
代码如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;//调用外部库,需要引用命名空间
using System;
using UnityEngine.UI;
public class UseWindowsMessagePop : MonoBehaviour
{
//引入dll
[DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr handle, String message, String title, int type);//具体方法
public Transform ButtonsParent;//按钮组
private int returnNumber; //返回值
private void Start()
{
int i = 0;
foreach (Transform item in ButtonsParent)
{
if (item.GetComponent<Button>())
{
item.name = item.GetChild(0).GetComponent<Text>().text = i.ToString();
item.GetComponent<Button>().onClick.AddListener(() => Button(int.Parse(item.name)));
i++;
}
}
}
/// <summary>
/// 按钮对应弹框
/// </summary>
/// <param name="index"></param>
private void Button(int index)
{
Debug.Log("click:" + index);
switch (index)
{
case 0:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-0:返回值均:1", "确认", 0);
print(returnNumber);
break;
case 1:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-1:确认:1,取消:2", "确认|取消", 1);
print(returnNumber);
break;
case 2:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-2:中止:3,重试:4,忽略:5", "中止|重试|忽略", 2);
print(returnNumber);
break;
case 3:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-3:是:6,否:7,取消:2", "是 | 否 | 取消", 3);
print(returnNumber);
break;
case 4:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-4:是:6,否:7", "是 | 否", 4);
print(returnNumber);
break;
case 5:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-5:重试:4,取消:2", "重试 | 取消", 5);
print(returnNumber);
break;
case 6:
returnNumber = MessageBox(IntPtr.Zero, "Chinar-6:取消:2,重试:10,继续:11", "取消 | 重试 | 继续", 6);
print(returnNumber);
break;
}
}
}