Unity调用Windows弹提示框
本文提供全流程,中文翻译。
Chinar坚持将简单的生活方式,带给世人!
(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例)
Chinar—— 心分享、心创新!
助力实现Unity开发的PC端项目,实现Windows内置弹窗
为新手节省宝贵的时间,避免采坑!
Chinar教程效果:
全文高清图片,点击即可放大观看(很多人竟然不知道)
1
Hint Frame —— 提示框
Chinar在全网搜索了许多资料,发现在于Windows的交互上很多都是空白的
而且大多数,都是描述不清。许多大神们也都没把实现流程写完全,导致明明比较简单的东西,新手非常难理解
那么Chinar在这里就做个总结,很简的实现弹框,大神们见笑!
Chinar写的非常细致,并且连返回值都一一列举出来,节省大家时间,不用一一测试!
在开发PC端项目时,有时我们需要提醒用户进行相关操作 / 提示用户下一步该如何操作
那么我们就需要与Windows进行交互,来告诉Windows弹个框出来
去提醒用户,确认/取消 是/否
2
Scripts —— 脚本
为了方便大家看到效果,我搭建了一个简单的UI,把所有弹框的效果都一一列举
层次列表与UI结构如下:
1.这里我介绍Unity中需要外部引用库,调用其中的函数实现弹框
先创建一个脚本ChinarMessage,不用继承Mono
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.另外,我们再创建一个脚本ChinarWindowsMessage挂载空物体上
将7个Button对应添加到数组中
using System; //引用命名空间下
using UnityEngine;
using UnityEngine.UI;
///
/// Windows消息框
///
public class ChinarWindowsMessage : MonoBehaviour
{
public Button[] Buttons; //按钮组
private int returnNumber; //返回值
private void Start()
{
for (int i = 0; i < Buttons.Length; i++)//动态绑定
{
var i1 = i;
Buttons[i].onClick.AddListener(() => Button(i1));
}
}
///
/// 9个按钮对应弹框
///
///
private void Button(int index)
{
switch (index)
{
case 0:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-0:返回值均:1", "确认", 0);
print(returnNumber);
break;
case 1:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-1:确认:1,取消:2", "确认|取消", 1);
print(returnNumber);
break;
case 2:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-2:中止:3,重试:4,忽略:5", "中止|重试|忽略", 2);
print(returnNumber);
break;
case 3:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-3:是:6,否:7,取消:2", "是 | 否 | 取消", 3);
print(returnNumber);
break;
case 4:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-4:是:6,否:7", "是 | 否", 4);
print(returnNumber);
break;
case 5:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-5:重试:4,取消:2", "重试 | 取消", 5);
print(returnNumber);
break;
case 6:
returnNumber = ChinarMessage.MessageBox(IntPtr.Zero, "Chinar-6:取消:2,重试:10,继续:11", "取消 | 重试 | 继续", 6);
print(returnNumber);
break;
}
}
}
4
Summarize —— 总结
点击运行,最终效果
提示:
点击提示框上边相应按钮的时候,会有不同的返回值
例如:第四个按钮,返回值分别为:6 / 7 / 2
可以根据返回值,自己写判定规则进行相关操作
如果需求的是英文提示框,贴心的Chinar为大家准备了中文教程
支持
May Be —— 搞开发,总有一天要做的事!
拥有自己的服务器,无需再找攻略!
Chinar 提供一站式教程,闭眼式创建!
为新手节省宝贵时间,避免采坑!
技术交流群:806091680 !Chinar 欢迎你的加入
END
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究
对于需要复制、转载、链接和传播博客文章或内容的,请及时和本博主进行联系,留言,Email: ichinar@icloud.com
对于经本博主明确授权和许可使用文章及内容的,使用时请注明文章或内容出处并注明网址
本文详细介绍了如何在Unity中调用Windows内置弹窗,提供全流程指导,包括不同类型的提示框及其返回值。通过创建ChinarMessage和ChinarWindowsMessage脚本,动态绑定按钮以实现不同弹框效果。适合新手快速理解和实现。

被折叠的 条评论
为什么被折叠?



