向对话框模态发送回调
本文关键字:回调 模态 对话框 | 更新日期: 2023-09-27 18:03:09
我正在为GUI系统开发一个可重用的库,我想在其中发送回调到显示对话框模态的静态方法。伪的,我想这样做:
void OnQuitButtonClick()
{
var buttons = new OrderedDictionary();
buttons.Add( "OK", (Action)(() => { Debug.Log("OK was pressed!"); }) );
buttons.Add( "Cancel", (Action)(() => { Debug.Log("Cancel was pressed!"); }) );
DialogBox.Show(
"Do you really want to quit?",
buttons
);
}
然后,在DialogBox类中:
public static Show(string message, OrderedDictionary buttons)
{
foreach (DictionaryEntry de in buttons)
{
var button = ...; // Instantiate the button object here.
button.GetComponent<Button>().onClick.AddListener( () => { de.Value; Close(); } );
}
}
void Close()
{
Destroy(gameObject); // Destroys "self".
}
这行不通,但这是我设法谷歌/阅读我的方式,我有一种感觉,我很接近。
任何关于如何进步的想法,也许使它更漂亮,将不胜感激!
谢谢!
您实际上并没有执行按钮处理程序代码。你可能想要改变这个
button.GetComponent<Button>().onClick.AddListener( () => { de.Value; Close(); } );
button.GetComponent<Button>().onClick.AddListener( () => {
((Action)de.Value).Invoke();
Close();
});