- Home /
How can i create a popup menu when a button is clicked?
i have a button on my game scene and i want to add functionality such that when the user clicks on the button, a menu pops up on the same scene. The menu can just have some basic text and another button. Please i need help on this
Well with the new UI system you shouldn't use GUI. To achive something like this first create your menu using UI elements (text/buttons and so on) make sure all said elements of the menu are parented by either an empty GameObject(gonna call it menu from now on) or if you want background on the menu an Image or panel. Place the said menu inside your canvas (if not done automatically) make sure in hierarchy it is below the button it "generates" it (more like makes it appear). Now create a script and attach it on any GameObject outside the canvas that would look like this-> //... public GameObject my$$anonymous$$enu;
public void Show$$anonymous$$enu(){
my$$anonymous$$enu.SetActive(true);
}
//...
Go to your Button and add the above method in the onclick field. Don't forget to turn off your menu and you are good to go. Of course with this, once you click on the button and the menu appears there is no way to turn it off so you need to add a way to close it but that depends on when you want it to get closed.
Hi @Milito007,
Done something similar to thing you look for. In my project I used Animator.
Firstly setup parent game object, with main button. Then as a child of it atache another witch will after click: "drive down", become visible (by changing alpha value from 0))... etc.
You will just need 2 animations, not looping, trigger based... one for opening menu, second for hiding it (could be opening with, speed -1).
Hope will help. Belive this way is easier than coding.
Answer by justinc847 · May 26, 2017 at 07:17 PM
You could create a boolean that checks whether or not the button has been pressed, and then utilize the OnGUI function:
public class ClickExample : MonoBehaviour
{
public Button yourButton;
//declare a boolean
public bool buttonisclicked;
void Start()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
//set to false on start, may not be necessary, I just can't remember if C# will return 1 or 0 for undefined booleans
buttonisclicked = false;
}
void TaskOnClick()
{
Debug.Log("You have clicked the button!");
//set our bool to true
buttonisclicked = true;
}
private void OnGUI()
{
//check if our button click boolean is true
if(buttonisclicked)
{
//create gui menu, refer to linked documentation to learn how to create gui elements via script, and learn about various styling techniques
}
}
}
Check out the documentation for UI buttons here
And the GUI Class here
Good luck with your project!
Do you know how to create a basic dropdown menu in the game? This doesn't answer the question at all; it only has info about how to detect a button was clicked
Answer by privatecontractor · Jan 20 at 07:50 PM
Hi @Milito007,
Done something similar to thing you look for. In my project I used Animator.
Firstly setup parent game object, with main button. Then as a child of it attache another-this will make present all actions after click: "drive down", become visible (by changing alpha value from 0))... etc.
You will just need 2 animations, not looping, trigger based... one for opening menu, second for hiding it (could be opening with, speed -1).
Hope will help. Belive this way is easier than coding.
Your answer
Follow this Question
Related Questions
Button on Panel is not responding 1 Answer
Missing 'Options' in my GUI Menu? 2 Answers
Placing button at corner using NGUI 2 Answers
iTween issue creating a gui. menu box 1 Answer
Playing different button animation after first animation plays 1 Answer