Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Milito007 · May 26, 2017 at 04:17 PM · buttonmenupopup

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PizzaPie · May 26, 2017 at 08:31 PM 0
Share

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.

avatar image privatecontractor · Jan 20 at 07:48 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaxLohMusic · Jan 19 at 11:42 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Playing different button animation after first animation plays 1 Answer

iTween issue creating a gui. menu box 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges