- Home /
Wait for user input in modal menu?
Let's say I have a function Foo() that prompts the user for keyboard input via Unity's UI system and returns a string. In this case, it's returning the player name.
string Foo() {
string name = "";
// Instantiate a prefab
GameObject nameInputModal = Instantiate(GenericInputModal) as GameObject;
nameInputModal.transform.SetParent (canvas.transform, false);
// Access button and add listener
InputDialogModal inputDialogModal = nameInputModal.GetComponent<InputDialogModal> ();
inputDialogModal.ContinueButton.onClick.AddListener (delegate {
name = inputDialogModal.inputField.text;
Destroy(nameInputModal);
});
return name;
}
What can I do to be able to do something like:
string test = GetPlayerName();
Debug.Log(test);
Because right now, the modal window opens fine but it blows right by the Debug.Log before the user responds (therefore it prints null).
Are yields/coroutines really what I'm looking for here or is there a better way to do it? I'm really trying to set up my modal windows in a way they return a type so I can use them in a variety of situations.
Comment