Unity doesn’t really support modal dialogs in the way you want, but there are some workaround, like setting the time scale to zero while it’s displayed, and adding a click-blocking later between the message box and everything underneath it.
There are also better ways to wait for the message box to be dismissed than a coroutine, such as a UnityEvent.
That said, if you just want to move your coroutine into the MessageBox class, it should be pretty simple: (warning: untested)
public class MessageBox : MonoBehaviour
{
private System.Action onDismiss;
public static void ShowMessage(string msg, System.Action onDismiss)
{
GameObject obj = (GameObject)Instantiate(Resources.Load("MsgBox"));
obj.GetComponentInChildren<Text>().text = msg;
MessageBox msg = obj.AddComponent<MessageBox>(); //or this component could be added to the prefab in the inspector.
msg.onCompleteonDismiss = onDismiss;
}
void Start()
{
StartCoroutine("Do");
}
IEnumerator Do()
{
yield return new WaitUntil(() => GlobalVariables.MsgBoxClicked);
//GlobalVariables is a separate class containing some static variables.
if (onCompleteonDismiss != null)
onCompleteonDismiss();
}
}
And then the code that wants to show the message can simply use
MessageBox.ShowMessage("This is a test message!", () => {
Debug.Log("Done!");
});
Basically, MonoBehaviour objects can’t exist on their own. They are components that must be attached to a GameObject, which is what the call to AddComponent is doing.