Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by valtteri_m · Dec 05, 2014 at 07:01 PM · javascriptguikeypause menuarrow-keys

Pause Menu controlled by keys

How could I make a GUI that has multiple buttons but you select them with your up and down keys and enter to press them, like console games you know o:

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kiwasi · Dec 06, 2014 at 10:03 AM

Upgrade to 4.6. Build a pause menu with the new UI tools. And you are done.

The new UI has built in support for this functionality.

Comment
Add comment · Show 2 · 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 valtteri_m · Dec 06, 2014 at 10:41 AM 0
Share

whats UI? Could you link me to a good thread or video

avatar image Kiwasi · Dec 06, 2014 at 06:48 PM 0
Share

Check out the learn section. There are a full set of tutorials on the new UI tools there.

avatar image
0

Answer by M-G-Production · Dec 06, 2014 at 07:56 PM

So here it is vultt3rim!

There is plenty of way to do it, but now I'll show you mine! I made it simple with bunch of comments to see what's actually going on! So this C# system will instantly work and maybe teach you some nice things!

BEFORE EVERYTHING: Create 2 C# scripts named "MenuController" and "ButtonScript" and copy/paste the scripts down there!

  1. Create an Empty Game Object and name it: "Menu Controller";

  2. Drag and drop the script named: "MenuController" in the new object you just created;

  3. Tell the MenuController script in Inspector how much you have buttons in your scene in "Buttons Count" field;

  4. Now, you will need to add the script "ButtonScript" to every button you have in your scene;

  5. Drag the textures in the "Button Normal" & "Button Hover" empty fields;

  6. Change the ID's of each individual buttons starting from 1 (Top First = 1, bottom = last one);

It will work perfectly if you take your time doing it well! If you don't like this way, it will be easy to remove. My pleasure!

Math

Here are the scripts!

MenuController.cs

 using UnityEngine;
 using System.Collections;
 
 public class MenuController : MonoBehaviour 
 {
     /*This script has been made by MGProduction!*/
 
     //THE HOW MANY BUTTONS YOU HAVE ON THE SCENE?
     public int buttonsCount;
 
     //THIS IS A VIRTUAL SELECTOR THAT DOES NOTHING ELSE THAN NAVIGATE BETWEEN DIFFERENT VALUES
     public int selector = 1;
 
     // Use this for initialization
     void Start () 
     {
         //I am setting the selector to 0
         selector = 1;
     }
     
     // Update is called once per frame
     void Update () 
     {
         //IF YOUR KEYBOARD IS NOT "AWSD", YOU CAN EASILY CHANGE THIS!
         if (Input.GetKeyUp(KeyCode.S))//DOWN
         {
             //IF MY SELECTOR IS LOWER THAN THE BUTTON COUNT
             if (selector < buttonsCount)
             {selector += 1;}
             //OTHERWISE, RESET IT TO 0!
             else
             {selector = 1;}
         }
 
         //SAME THING BUT INVERTED
         if (Input.GetKeyUp(KeyCode.W))//UP
         {
             if (selector > 1)
             {selector -= 1;}
             else
             {selector = buttonsCount;}
         }
     }
 }
 

ButtonScript.cs

 using UnityEngine;
 using System.Collections;
 
 public class ButtonScript : MonoBehaviour {
 
     /*This script has been made by Mathieu from MGProduction!*/
 
     //Textures selection
     public Texture2D buttonNormal, buttonHover;
 
     //The ID int is about the action the button is going to do!
     //GO SEE THE Selected() function at the bottom
     public int ID;
 
     //IT WILL REQUIRES THE MENU CONTROLLER OBJECT
     private GameObject menuController;
 
     //WILL REFER TO THE MENUCONTROLLER SCRIPT
     private int selector;
 
     //IF THE BUTTON IS SELECTED
     private bool selected;
 
     void Start()
     {
         //I'M SEARCHING THE MENU CONTROLLER BY HIS NAME
         menuController = GameObject.Find ("Menu Controller");
 
         //Let's start with the normal texture
         gameObject.guiTexture.texture = buttonNormal;
     }
 
     // Update is called once per frame
     void Update () 
     {
         //Check it only if it changes! POWER & RAM SAVED TECHNIC
         if (selector != menuController.GetComponent<MenuController> ().selector)
         {
             //Accessing the MenuController script
             selector = menuController.GetComponent<MenuController> ().selector;
         }
 
         //If the MENUCONTROLLER.SELECTOR = MyID then
         if (selector == ID)
         {selected = true;}
         else
         {selected = false;}
 
         //IF YOU ARE SELECTED, RUN FUNCTION Selected()
         if (selected)
         {Selected();}
         else
         {
             //CHANGING TEXTURE 1 TIME
             if (gameObject.guiTexture.texture == buttonHover)
             {gameObject.guiTexture.texture = buttonNormal;}
         }
     }
 
     void OnMouseOver()
     {
         //Mouse Over dominate the selector!
         if (menuController.GetComponent<MenuController> ().selector != ID)
         {menuController.GetComponent<MenuController> ().selector = ID;}
     }
 
     void Selected()
     {
         //CHANGING TEXTURE 1 TIME
         if (gameObject.guiTexture.texture == buttonNormal)
         {gameObject.guiTexture.texture = buttonHover;}
 
         //THIS IS WHERE YOU NEED TO MAKE THE BUTTONS DO SOMETHING!
         if (Input.GetKeyUp(KeyCode.Return) || Input.GetMouseButtonUp(0))
         {
             //THIS IS WHERE YOU WANT TO ACTUALLY DO SOMETHING WITH YOUR BUTTONS!
             switch (ID)
             {
             case 1:
                 //DO WHAT YOU WANT THE TOP FIRST BUTTON TO DO?
                 //EXEMPLES:
                 //Application.LoadLevel("MyAwesomeLevel");
                 Debug.Log("Button "+ID.ToString()+" is in action");
                 break;
 
             case 2:
                 //DO WHAT YOU WANT THE SECOND BUTTON TO DO
                 Debug.Log("Button "+ID.ToString()+" is in action");
                 break;
 
             case 3:
                 //ETC
                 Debug.Log("Button "+ID.ToString()+" is in action");
                 break;
 
             case 4:
                 Debug.Log("Button "+ID.ToString()+" is in action");
                 break;
 
             case 5:
                 //IF EXIT BUTTON:
                 //Application.Quit();
                 Debug.Log("Button "+ID.ToString()+" is in action");
                 break;
 
             //CLEAN UP THIS SWITCH WITH WHAT YOU NEED! LESS OR MORE?
             //IF YOU HAVE ONLY 
             }
         }
     }
 }
Comment
Add comment · Show 2 · 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 Kiwasi · Dec 06, 2014 at 08:19 PM 0
Share

While technically correct, this is a lot of work, and should only be done if your current project is version constrained to an older version if Unity. From 4.6 this is all built in.

avatar image valtteri_m · Dec 07, 2014 at 09:06 AM 0
Share

I made an object and added the controller and the button scripts to it and it doesnt work, am I doing it wrong?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Pause Menu won't open... 1 Answer

Key for GUI.Button 2 Answers

Assign a hotkey to a GUI button? 3 Answers

How can i show the texture in the array? 2 Answers


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