简体   繁体   English

Unity - 制作一个等待用户输入的同步方法

[英]Unity - Make a Sync Method that waits for user input

I have basic knowledge of C# with WinForms and WPF .我对C#WinFormsWPF有基本的了解。 But I am new to Unity .但我是Unity的新手。 I want to make a series of Message Boxes those appear one after another(once user close current than next box will show).我想制作一系列消息框,它们一个接一个地出现(一旦用户关闭当前,下一个框就会显示)。 It is very easy to use MessageBox.Show("Hello World");使用MessageBox.Show("Hello World"); in WinForms .WinForms中。

void myFun(){
    MessageBox.Show("Hello World1");//First Box
    //Some Code
    MessageBox.Show("Hello World2");//Another

    MessageBox.Show("Hello World3");//Finally Results
}

In WinForms (or WPF ) Code after MessageBox.Show();WinForms (或WPF )中MessageBox.Show(); will not Executed before we Give input to close.在我们提供输入以关闭之前不会执行。

I want to make same MessageBox in unity with GameObject where code will not execute before GameObject is SetActive(False);我想与GameObject统一制作相同的MessageBox ,其中在GameObject为 SetActive(False) 之前不会执行代码; by user command(mouse Click on Background or Yes/No Buttons).通过用户命令(鼠标单击背景或是/否按钮)。

Thanks in Advance提前致谢

sorry for bad English抱歉英语不好

One of the best approaches becoming popular in Unity the last years is an Async approach .最近几年在 Unity 中流行的最佳方法之一是异步方法 So I highly recommend you to learn this to have even more powerful tool than Coroutines and to avoid Callback Hell in case of Action usage.所以我强烈建议你学习这个,以获得比协程更强大的工具,并在使用Action时避免回调地狱

Let's implement it this Async approach together.让我们一起实现这种异步方法。

Firstly, we need that MessageBox implementation.首先,我们需要 MessageBox 实现。 Let's say it has 2 buttons: Confirmation and Cancellation.假设它有 2 个按钮:确认和取消。 Here's it:这是它:

public MessageBox : MonoBehaviour
{
    [SerializeField]
    private Button _confirmationButton;
    [SerializeField]
    private Button _cancelButton;
    
    private TaskCompletionSource<bool> _tcs = new TaskCompletionSource<bool>();

    public void OnConfirmationButtonClick()
    {
        // we're passing `true` if user clicks `Confirm`
        _tcs.SetResult(true);
    }
    
    public void OnCancellationButtonClick()
    {
        // we're passing `false` if user clicks `Cancel`
        _tcs.SetResult(false);
    }

    public async Task<bool> ShowAsync()
    {
        gameObject.SetActive(true);
        
        // recreate an instance to not use the `SetResult` value of previous showing
        _tcs = new TaskCompletionSource<bool>();
        
        // the execution stops here ASYNCHRONOUSLY, so the UI thread is not blocked.
        // It just awaits until we set any result to `_tcs`
        await _tcs.Task;

        gameObject.SetActive(false);
    }
}

And now we can show a few message boxes and await for their input.现在我们可以显示一些消息框并等待他们的输入。 Moreover, we may know what exactly button was clicked: Confirmation or Cancellation:此外,我们可能知道究竟点击了什么按钮:确认或取消:

public async Task YourOwnMethodAsync()
{
    // Let's assume you've instantiated 3 message boxes already and have their references

    var resultOfFirstMessageBox = await _messageBox1.ShowAsync();
    Debug.Log($"Showing the first message box shown. Result: {resultOfFirstMessageBox}");
    
    var resultOfSecondMessageBox = await _messageBox2.ShowAsync();
    Debug.Log($"Showing the second message box shown. Result: {resultOfSecondMessageBox}");

    var resultOfThirdMessageBox = await _messageBox3.ShowAsync();
    Debug.Log($"Showing the third message box shown. Result: {resultOfThirdMessageBox}");
}

If you need even more detailed description, just let me know.如果您需要更详细的描述,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM