hi guys, this is my first post in this forum, I am a novice, I have been working with Unity for about 1-2 months
this is my code ( messageBox class : create messageBox, and show/hide, ez code ) :
http://s12.picofile.com/file/8403990450/MessageBox.cs.html
i use this class like this :
messageBox.Show("my Title", "my Text", MessageBox.MessageBoxButtons.OkCancel);
output :
MY Problem :
i want unity wait until user press any button of messageBox but… ( i don’t know how do this )
I saw other posts, And I wrote the following code :
but in output i see :
A0
A1
A5
my problem is that i see this “A5” in output
Now what should I do if I want to exit the “ImportExcelFile” function or execute the continuation of the function code?
public void ImportExcelFile()
{
Debug.Log("A0");
StartCoroutine(Show_MessageBox());
Debug.Log("A5");
// Other codes for this function
}
IEnumerator Show_MessageBox()
{
messageBox.Show("my Title", "my Text", MessageBox.MessageBoxButtons.OkCancel);
Debug.Log("A1");
while (messageBox.result == MessageBox.MessageBoxResult.Unknown) yield return null; // wait
Debug.Log("A2");
if (messageBox.result == MessageBox.MessageBoxResult.Cancel)
{
// Exit from "ImportExcelFile" function
Debug.Log("A3");
}
else // OK
{
// Continue the "ImportExcelFile" function codes
Debug.Log("A4");
}
}
and i don’t want to use callback-functions like this :
Debug.Log("A0");
messageBox.Show(
"Title",
"Text",
MessageBox.MessageBoxButtons.OkCancel,
() => { Debug.Log("OK"); },
() => { Debug.Log("Cancel"); }
);
Debug.Log("A1");
output :
A0
OK
A1
Seriously? This is 2020 you know… I would just have it optionally accept another argument, a System.Func<bool>
argument that defaults to null, and then execute it (if supplied) from within the MessageBox’s Update() function, and if it returns true, and shut your message box.
If you want to invoke the box so you can dismiss with any key, just supply this delegate:
() => { return Input.anyKeyDown; }
I mean it doesn’t get much simpler than that. There’s a reason we reach for delegates like that. It’s really good!
Thank you Mr. Kurt-Dekker
this is my function that i need it :
function ImportExcelFile()
{
// [1]
if (we have data in array)
{
messageBox.Show(
"Title",
"Delete array?",
MessageBox.MessageBoxButtons.OkCancel,
() => {
Debug.Log("Yes");
// Delete Array
},
() => {
Debug.Log("No");
// Save Array then Delete Array
}
);
}
// [2]
// Continue if at least one airport was defined in the settings, otherwise exit this function
// [3]
// Show OpenFileDialog --> wait until user select a file ---> if user pressed "Load" button ---> read that file address
}
When my function was executed, The code [1] is executed and shows the message box to the user, But before the user can press a button, Codes [2] and [3] have been executed! This is my problem and I do not know how it is solved.
i solved my problem 
i must put all my code in a IEnumerator function
private void Button_ImportExcelFile_OnClick()
{
StartCoroutine(_Button_ImportExcelFile_OnClick());
}
private IEnumerator _Button_ImportExcelFile_OnClick()
{
// first close port, then import...
if (uartController.serialPort1.IsOpen)
{
yield return MessageBox.Show("Warning!", "first close port, then import...", MessageBox.MessageBoxButtons.OK);
}
// There are no airports in the settings!
if (uiSettingClass.data.airports.Count == 0)
{
yield return MessageBox.Show("Warning!", "please define atleast 1 airport [ in setting ]", MessageBox.MessageBoxButtons.OK);
}
if (gManager.gpsRecords.Count != 0)
{
yield return MessageBox.Show("Warning!", "we have some records in array, this records deleted [or you whant save it]?", MessageBox.MessageBoxButtons.OkCancel);
if (MessageBox.result == MessageBox.MessageBoxResult.Cancel) yield break;
}
// Open Record File [ Exel ]
// ...
}
You want a thing done,do it yourself ! 