Unity调用Windows弹框(User32.dll/System.Windows.Forms.dll)

在开发 PC 端项目时,有时我们需要提醒用户进行相关操作 / 提示用户下一步该如何操作

那么我们就需要与 Windows 进行交互,来告诉 Windows 弹个框出来

去提醒用户,确认/取消 是/否 

这里有两种方法:

1.调用User32.dll外部库,实现弹框

2.调用System.Windows.Forms.dll 外部库,实现弹框

一、调用User32.dll外部库,实现弹框
这里我介绍 Unity 中需要外部引用库,调用其中的函数实现弹框
1.先创建一个脚本 ChinarMessage ,不用继承 Mono
 
//-----------------------
// @Author GarFey
// @date 20190612
// @version 1.0
//-----------------------
using System;
using System.Runtime.InteropServices;//调用外部库,需要引用命名空间

///
/// 为了调用外部库脚本
///
public class ChinarMessage
{
    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);
    //具体方法
}
2.在Unity逻辑代码中,你需要弹出提示框的地方调用提示框
提示:
点击提示框上边相应按钮的时候,会有不同的返回值
2.1
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-0:返回值均:1", "确认", 0);
                print(returnNumber);

 2.2

returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-1:确认:1,取消:2", "确认|取消", 1);
                print(returnNumber);

 2.3

returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-2:中止:3,重试:4,忽略:5", "中止|重试|忽略", 2);

                print(returnNumber);

 2.4

returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-3:是:6,否:7,取消:2", "是 | 否 | 取消", 3);

                print(returnNumber);

 2.5

returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-4:是:6,否:7", "是 | 否", 4);

                print(returnNumber);

2.6

returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-5:重试:4,取消:2", "重试 | 取消", 5);

                print(returnNumber);

 2.7

returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-6:取消:2,重试:10,继续:11", "取消 | 重试 | 继续", 6);

                print(returnNumber);

二、调用System.Windows.Forms.dll 外部库,实现弹框
这里我们可以调用 System.Windows.Forms.dll 外部库,很简单的就可以实现

获取到的dll后,放到Unity的Plugine目录下。
注意:
由于 Unity的机制 ,一定要新建一个空文件夹“Plugins”
将 System.Windows.Forms.dll 文件放到该文件夹下才行
在Unity逻辑代码中,你需要弹出提示框的地方调用提示框
在你的调用的类中,添加如下的引用
using MsgBoxBase = System.Windows.Forms.MessageBox; //引用命名空间下消息类
using WinForms = System.Windows.Forms;              //引用命名空间
2.1
               
//Asterisk:包含一个符号,该符号在圆圈中包含小写字母 i。
//OK:OK按钮

MsgBoxBase.Show("Chinar,OK按钮,灯泡图标", GetType().Name, WinForms.MessageBoxButtons.OK, WinForms.MessageBoxIcon.Asterisk);

2.2

//Error:包含一个符号,该符号包含一个红色背景圆圈,圆圈中为白色 X 符号。
//AbortRetryIgnore:包含中止、 重试和忽略按钮。

MsgBoxBase.Show("Chinar,中止|重试|忽略,错误图标", GetType().Name, WinForms.MessageBoxButtons.AbortRetryIgnore, WinForms.MessageBoxIcon.Error);

 2.3

//Exclamation:包含一个符号,该符号包含一个黄色背景三角形,三角形中为感叹号。
//OKCancel:中包含确定和取消按钮。

MsgBoxBase.Show("Chinar,确定|取消,三角感叹号", GetType().Name, WinForms.MessageBoxButtons.OKCancel, WinForms.MessageBoxIcon.Exclamation);

 2.4

//Hand: 包含一个符号,该符号包含一个红色背景圆圈,圆圈中为白色 X 符号。
//RetryCancel:重试和取消按钮

MsgBoxBase.Show("Chinar,重试 | 取消,(同错误图标)", GetType().Name, WinForms.MessageBoxButtons.RetryCancel, WinForms.MessageBoxIcon.Hand);

 2.5

//Information:包含一个符号,该符号在圆圈中包含小写字母 i。
//YesNo:是和否按钮

switch (MsgBoxBase.Show("Chinar,是 | 否,灯泡", GetType().Name, WinForms.MessageBoxButtons.YesNo, WinForms.MessageBoxIcon.Information))
{
    case WinForms.DialogResult.Yes:
         print("对");
         break;
    case WinForms.DialogResult.No:
         print("错");
         break;
    case WinForms.DialogResult.None:
         print("无");
         break;
 }

 2.6

//None:未包含符号。
//YesNoCancel:是、否、取消按钮

MsgBoxBase.Show("Chinar,是 | 否 | 取消,无图标", GetType().Name, WinForms.MessageBoxButtons.YesNoCancel, WinForms.MessageBoxIcon.None);

 2.7

//Question:包含一个符号,该符号包含一个圆圈,圆圈中为问号。 不再建议使用问号消息图标,因为这种图标无法清楚地表示特定类型的消息,并且作为问题的消息表述可应用于任何消息类型。 此外,用户可能会将问号消息符号与帮助信息混淆。 因此,不要在中使用问号消息符号。 系统继续支持它包含的内容,只为满足反向兼容性。

MsgBoxBase.Show("Chinar,OK按钮,问号?", GetType().Name, WinForms.MessageBoxButtons.OK, WinForms.MessageBoxIcon.Question);

//Stop:包含一个符号,该符号包含一个红色背景圆圈,圆圈中为白色 X 符号。
                MsgBoxBase.Show("Chinar,OK按钮,停止", GetType().Name, WinForms.MessageBoxButtons.OK, WinForms.MessageBoxIcon.Stop);
【图丢失】
//Warning:包含一个符号,该符号包含一个黄色背景三角形,三角形中为感叹号。
                MsgBoxBase.Show("Chinar,OK按钮,警告", GetType().Name, WinForms.MessageBoxButtons.OK, WinForms.MessageBoxIcon.Warning);
【图丢失】
提示:
点击提示框上边相应按钮的时候,会有不同的返回值
WinForms.DialogResult.Yes: / No / None /Cancel 等等
上边代码中,我写有一个例子,可以根据情况自己写判定规则
 switch (MsgBoxBase.Show("Chinar,是 | 否,灯泡", GetType().Name, WinForms.MessageBoxButtons.YesNo, WinForms.MessageBoxIcon.Information))
                {
                    case WinForms.DialogResult.Yes:
                        print("对");
                        break;
                    case WinForms.DialogResult.No:
                        print("错");
                        break;
                    case WinForms.DialogResult.None:
                        print("无");
                        break;
                }

猜你喜欢

转载自blog.csdn.net/u013774978/article/details/129847199#comments_26818321
今日推荐