【问题标题】:How to create MessageBox in Unity without event/delegate callback?如何在没有事件/委托回调的情况下在 Unity 中创建 MessageBox?
【发布时间】:2021-12-20 13:58:00
【问题描述】:

MessageBox 中有一部分需要在 Unity 中重新创建。

DialogResult result = MessageBox.Show(/*parameters*/);
//When code runs - appears MessageBox and next lines of code WAIT for result
//Only if button is pressed - code proceeds next lines.
if (result == DialogResult.Yes) { DoIt(); }
else { DoNotDoIt(); }

我已经看到了一些关于 Action 回调的实现。 在那个实现中 - MessageBox 具有 Action 字段,并且在 MessageBox.Show() 方法中,您应该将委托添加到适当的 Action 作为参数。 我不能在我的情况下使用它。我需要确保设置了对话框结果,然后才能继续运行代码。

【问题讨论】:

  • 我应该为此使用多线程吗?
  • 我对多线程说不,尤其是因为你需要它停止。然而。我会问。为什么不能使用回调?个人 id 在协程中执行此操作,您可以等到说 messagebox.result!=null

标签: c# multithreading unity3d messagebox


【解决方案1】:

您可以在更新中运行您的代码。 DialogResult 可能有 3 种状态,None、Yes 和 No。使用 None,您什么都不做,Update 返回并在下一帧返回:

void Update()
{
    DialogResult result = MessageBox.Show(/*parameters*/);
    if (result == DialogResult.Yes) { DoIt(); }
    else if(result == DialogResult.No) { DoNotDoIt(); }
}

其他方式是协程,和更新基本相同:

IEnumerator DialogSequence()
{         
     while(MessageBox.Show(/*parameters*/)== DialogResult.None) { yield return null;}
     DialogResult result = MessageBox.Show(/*parameters*/);
     if (result == DialogResult.Yes) { DoIt(); }
     else if(result == DialogResult.No) { DoNotDoIt(); }
}

【讨论】:

  • 嗯...也许是这样? private static DialogResult s_result;private IEnumerator TryToDoStuff(){//some codes_result = DialogResult.NONE;yield return MessageBox.Show();if (s_result == DialogResult.YES){{DoIt();}}
  • 我应该如何在 cmets 中格式化代码? :(
  • 您应该添加 MessageBox 代码。
  • 你不应该使用Update或者这个......你不想每帧都调用DialogResult result = MessageBox.Show!协程方法更好目前它只等待一个帧!你会想做while(result == DialogResult.None){ yield return null; },然后你可以使用switch - case 来完成其余的工作;)
  • 最初是为了避免回调(原因不明),然后每帧都要轮询消息框状态。否则,您将永远无法获得新状态。协程确实存在应该在一个while循环中的问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 2023-01-13
  • 2012-09-13
  • 1970-01-01
相关资源
最近更新 更多