- Home /
Issues building an in-game MsgBox
Hi guys, i want to build a Message Box that i can use at any point, however i found some issues.
First here is how i do it:
public class MsgBox : MonoBehaviour
{
void Update ()
{
}
public static void CreateMsgBox(string t, string b)
{
GameObject msgbox = Instantiate(Resources.Load("UI/Prefab/MessageBox")) as GameObject;
msgbox.transform.SetParent((GameObject.Find("Canvas").transform), false);
Text[] info = msgbox.GetComponentsInChildren<Text>();
info[0].text = t;
info[1].text = b;
}
}
And this is how i call it:
MsgBox.CreateMsgBox("Since this is the first time entering the Offline Netsphere, you must create a user.","Confirm");
Here is the thing though, I want to modify what happens after the "Confirm"(For example) button is clicked, And get it closed when clicked too.
So i thought adding an OnClick() on the class but that didn't work obviously,
I guess i can solve the closing thing if add it to the prefab directly(didn't think about it), but that's not my main issue anyway.
How can i add an event after clicking the Button of the messagebox? I guess you could check in the Update Function if it gets destroyed, but seems kind of messy to me.
Any suggestions of how can i handle this? I would really appreciate it
Thanks in advance :)
Answer by Seyren · Jun 09, 2015 at 09:49 PM
Thanks for all the help guys! this is what i figured out after your two suggestions!
I made the MsgBox able to return a Button, so this way:
Button b = MsgBox.CreateMsgBox("Since this is the first time entering the Offline Netsphere, you must create a user.","Confirm");
b.onClick.AddListener(CreateName);
void CreateName()
{
Debug.Log("YOU ARE A SQUID NOOOOOOOOOOW");
}
I can add any function from anywhere i want :D
I got inspired by both the suggestions and the video guide, thanks you both :P
Answer by Bioinformatizer · Jun 09, 2015 at 12:15 AM
One way to get a button to help in this context is to add a new public method to your script and reference this with a button that is already on the prefab.
Let's say that you have a button on the prefab already.
You then need a public reference on your script, something like :
public void ThisButton(){
Debug.Log("This button was clicked");
}
Now you go to the inspector of the button on the prefab and at the bottom add and event with the '+' symbol and select onClick().
Put the prefab containing the script into the select object.
Lastly you select the script in the function select and pick your method ThisButton() and it will print your debug (or whatever else you would like within the block).
Good Luck with the buttons!
Would this mean that i have to add a prefab of the button everytime i want to do something different? just asking if i could do ThisButton() function outside the prefab to prevent this, like passing it as a parameter or something, since i want to use the $$anonymous$$essage box for a lot of things, this is how it looks like: http://puu.sh/iiba7/b6e74e1711.jpg thanks a lot on the rest though :P
As long as you have the link established between that button and the code, you should be able to handle a wide variety of conditions. A way to do this is using the Switch method.
You may have to pass an additional parameter to the msgBox call you used above though.
Try and give the Create$$anonymous$$essageBox another parameter int boxCase. You would need to declare the int buttonCase above Update as normal and then set 'buttonCase = boxCase'. Lastly you could use the buttonCase for a Swtich like this :
public void ThisButton()
{
switch (buttonCase)
{
case 5:
StartLevelFuntion();
break;
case 4:
OpenNew$$anonymous$$essageBox();
break;
case 3:
int seven = 5;
int eleven = 5;
Debug.Log(seven+eleven);
break;
case 2:
print ("message complete");
break;
case 1:
InstantiateClownBehindYou();
break;
default:
print ("$$anonymous$$essageCase is not set right");
break;
}
}
}
Answer by xSmurfKasketter · Jun 09, 2015 at 12:18 PM
You could use events, Unity live training have a great lesson to make a modal UI system, more or less exactly what you would like and like this, you save some overhead and can use it everywhere, this is the link: http://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/modal-window
You can ofcourse add the method to the button directly like Bioinformatizer suggested, however this couples it quite tightly with the script instead of having something you can use everywhere