You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity项目中创建非模态Windows风格MessageBox弹窗

解决Unity桌面端非阻塞Windows风格弹窗问题

方案1:异步调用Windows原生MessageBox(非阻塞)

原生MessageBox是模态阻塞的,但可以通过子线程异步调用,避免卡住Unity主线程。代码示例:

using System.Threading;
using UnityEngine;
using System.Runtime.InteropServices;

public class NonBlockingMsgBox : MonoBehaviour
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);

    public void ShowNonBlocking(string message, string title)
    {
        Thread msgThread = new Thread(() =>
        {
            MessageBox(IntPtr.Zero, message, title, 0);
        });
        msgThread.Start();
    }
}

如果需要获取用户点击结果(确定/取消),可以用AutoResetEvent做线程同步,将结果传回主线程后再处理,注意不要在子线程直接操作Unity对象。

方案2:修复WinForms引用并实现非阻塞弹窗

如果想用WinForms的标准Windows风格弹窗,先解决引用问题:

  1. 打开Unity Player Settings → Other Settings,将Api Compatibility Level设置为.NET Framework
  2. 在Visual Studio中右键项目 → 添加引用,勾选System.Windows.Forms和System.Drawing
    之后就可以用子线程启动非阻塞Form:
using System.Windows.Forms;
using UnityEngine;
using System.Threading;

public class WinFormsPopup : MonoBehaviour
{
    public void ShowPopup(string message, string title)
    {
        Thread formThread = new Thread(() =>
        {
            Form popupForm = new Form
            {
                Text = title,
                Size = new System.Drawing.Size(320, 160),
                StartPosition = FormStartPosition.CenterScreen
            };

            Label msgLabel = new Label
            {
                Text = message,
                Dock = DockStyle.Fill,
                TextAlign = System.Drawing.ContentAlignment.MiddleCenter
            };
            popupForm.Controls.Add(msgLabel);

            Button okBtn = new Button
            {
                Text = "确定",
                Dock = DockStyle.Bottom
            };
            okBtn.Click += (s, e) => popupForm.Close();
            popupForm.Controls.Add(okBtn);

            Application.Run(popupForm);
        });
        formThread.SetApartmentState(ApartmentState.STA);
        formThread.Start();
    }
}

子线程启动的Form不会阻塞Unity主线程,游戏逻辑和角色交互可以正常运行。

方案3:纯Unity实现仿Windows风格非阻塞弹窗

用UGUI制作完全匹配Windows风格的自定义弹窗,全程在Unity主线程运行,无外部依赖:

  1. 制作弹窗预制体:添加Canvas → Panel(仿Windows窗口的边框、标题栏)→ Text(显示消息)→ Button(确认/取消)
  2. 用代码控制弹窗的显示隐藏:
using UnityEngine;
using UnityEngine.UI;

public class CustomWindowsPopup : MonoBehaviour
{
    [SerializeField] private GameObject popupPrefab;

    public void ShowPopup(string message)
    {
        GameObject popup = Instantiate(popupPrefab, transform.root);
        Text msgText = popup.GetComponentInChildren<Text>();
        Button okBtn = popup.GetComponentInChildren<Button>();

        msgText.text = message;
        okBtn.onClick.AddListener(() => Destroy(popup));
    }
}

这种方式最灵活,可自定义程度高,且完全不会阻塞游戏逻辑,生物角色可以正常和玩家交互。

内容的提问来源于stack exchange,提问作者Walter Medly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:10:28