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 ZaxOnTheBeach · Jun 02, 2013 at 09:52 PM · guitogglegamepad

Navigating GUI with gamepad on GUI.Toggle

I've been trying to add to this script provided by Farfarer in 2011 so that I can navigate a GUI menu with a gamepad and toggle a GUI.Toggle button on and off with a gamepad BUTTON vs. the left mouse-button click.

An edit to this script done by chelnok is closest to what I want because it allows me to execute a GUI button with the gamepad but I can't get it to work with a toggle.

Here is the script;

     // Array of menu item control names.
     var menuOptions = new String[4];
      
     menuOptions[0] = "Tutorial";
     menuOptions[1] = "Play";
     menuOptions[2] = "High Scores";
     menuOptions[3] = "Exit";
      
     // Default selected menu item (in this case, Tutorial).
      
     var selectedIndex = 0;
      
     // Function to scroll through possible menu items array, looping back to start/end depending on direction of movement.
      
     function menuSelection (menuItems, selectedItem, direction) {
      
         if (direction == "up") {
             if (selectedItem == 0) {
                 selectedItem = menuItems.length - 1;
             } else {
                 selectedItem -= 1;
             }
         }
        
         if (direction == "down") {
             if (selectedItem == menuItems.length - 1) {
                 selectedItem = 0;
             } else {
                 selectedItem += 1;
             }
         }
      
         return selectedItem;
     }
      
      
      
     function Update ()
     {
         if (Input.GetKeyDown("down")) {
             selectedIndex = menuSelection(menuOptions, selectedIndex, "down");
         }
      
         if (Input.GetKeyDown("up")) {
             selectedIndex = menuSelection(menuOptions, selectedIndex, "up");
         }
      
         if (Input.GetKeyDown("space")) {   
             handleSelection();
         }
     }
      
     function handleSelection()
     {
         GUI.FocusControl (menuOptions[selectedIndex]);
        
         switch (selectedIndex)
         {
             case 0:  
                    Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
                    break;
             case 1:
                    Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
                    break;
             case 2:
                    Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
                    break;
             case 3:
                    Debug.Log("Selected name: " + GUI.GetNameOfFocusedControl () + " / id: " +selectedIndex);
                    break;         
             default:
                    Debug.Log ("None of the above selected..");
                    break;
         }
     }
      
     function OnGUI ()
     {
         GUI.SetNextControlName ("Tutorial");
         if (GUI.Button(Rect(10,70,170,30), "Button 1 (tutorial,id:0)")) {
             selectedIndex = 0;
             handleSelection();
         }
      
         GUI.SetNextControlName ("Play");
         if (GUI.Button(Rect(10,100,170,30), "Button 2 (play, id:1)")) {
             selectedIndex = 1;
             handleSelection();
         }
      
         GUI.SetNextControlName ("High Scores");
         if (GUI.Button(Rect(10,130,170,30), "Button 3 (high scores, id:2)")) {
             selectedIndex = 2;
             handleSelection();
         }
      
         GUI.SetNextControlName ("Exit");
         if (GUI.Button(Rect(10,160,170,30), "Button 4 (exit, id:3)")) {
             selectedIndex = 3;
             handleSelection();
         }
      
         GUI.FocusControl (menuOptions[selectedIndex]);
     }

Any help with this would be very much appreciated!! :)

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

0 Replies

· Add your reply
  • Sort: 

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

14 People are following this question.

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

Related Questions

Whats wrong with my GUI.Toggle? 2 Answers

Is there a multiple selection ugui toggle group? 1 Answer

Control script generated buttons with a Gamepad 0 Answers

Creating a menu for gamepad? 1 Answer

GUI toggle on click, unless on GUI. 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