【发布时间】:2020-01-18 23:24:47
【问题描述】:
我正在尝试制作一个简单的消息框,以帮助指导我的输出的可读性。我在 Unity 中使用 C#。
原因是消息通常很长、令人费解或偶尔需要回复。
我正在尝试使用简单的bool 标志调用DisplayMessage(string _message) 方法,以使其关闭并继续。但无论我做什么,它要么冻结(谢谢while 无限循环)要么传递到主线程
IEnumerables 不是我的强项之一,除非我遗漏了一些基本和明显的东西(我非常愿意承认这种可能性),否则协程仍然无法正确支持 DoDisplay(),直到我获取按钮输入
我花了几个小时寻找,但我的眼睛对 Systems.Threading 解决方案的选项感到茫然
public Class MainTurn: MonoBehavior {
public GameControl gCon;
public void OnPhaseOne(GameControl gCon) {
// *** EVENT CARDS ***
DisplayManager dMan = GameObject.FindGameObjectWithTag("Display Manager").GetComponent<DisplayManager>();
dMan.DoDisplay("Take 2 Cards, update Red Count");
// Check for 6+ Event Cards
if (gCon.Cards >= 6) {
dMan.DoDisplay("Discard a Card\n Discard Order:\n1. 2. 3. 4.");
}
// Allow for Exchange
if (gCon.Cards == 2) {
dMan.DoDisplay("Exchange Card as Required");
}
}
}
public class DisplayManager : MonoBehaviour {
public GameObject DisplayPanel;
public Text Message_Text;
public Button Complete_Button;
private bool _isComplete;
public void DoDisplay(string _displayMessage)
{
// Activate Panel
DisplayPanel.SetActive(true);
// Set flag and display message
_isComplete = false;
Message_Text.text = _displayMessage;
// Wait until Complete
while (_isComplete == false)
{
Debug.Log("Waiting");
}
//Clear and close
Message_Text.text = "";
DisplayPanel.SetActive(false);
}
// A simple Button press to clear the flag
public void OnComplete()
{
_isComplete = true;
}
}
【问题讨论】: