• 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
Question by ChronoRider · Jun 25, 2014 at 02:09 PM · guimousejoystickcontrolgamepad

Using Gamepad to access onGUI menus?

Hi everyone, the first scene in my game requires access with a mouse to start a new game or load a saved game.

I need to make the GUI accessible with a controller, but I after a lot of browsing and googling I am very stuck.

Here is the working code so far,

 using UnityEngine;
 using System.Collections;
 
 public class LoadGame : MonoBehaviour {
     public Texture2D[] playerPic = new Texture2D[1];
     public int saveSlot = 0;
     public GameObject[] playerPrefab = new GameObject[1];
     public string startScene = "Stage1";
     private Vector3 lastPosition;
     private bool  menu = true;
     private bool charSelectMenu = false;
     private bool loadMenu = false;
     // Use this for initialization
     void Start () {
         Time.timeScale = 1.0f;
         menu = true;
         charSelectMenu = false;
         loadMenu = false;
     }
     
     void  OnGUI (){
         if(menu){
             if (GUI.Button ( new Rect(Screen.width / 2 - 150,185,300,120), "New Game")) {
                 charSelectMenu = true;
                 loadMenu = false;
                 menu = false;
             }
             if (GUI.Button ( new Rect(Screen.width / 2 - 150,325,300,120), "Load Game")) {
                 loadMenu = true;
                 menu = false;
                 charSelectMenu = false;
             }
         }
         if(loadMenu){
             GUI.Box ( new Rect(Screen.width / 2 - 250,170,500,400), "Menu");
             if (GUI.Button ( new Rect(Screen.width / 2 + 185,175,30,30), "X")) {
                 loadMenu = false;
                 charSelectMenu = false;
                 menu = true;
             }
             //---------------Slot 1 [ID 0]------------------
             if(PlayerPrefs.GetInt("0PreviousSave") > 0){
                 if (GUI.Button ( new Rect(Screen.width / 2 - 200,205,400,100), PlayerPrefs.GetString("0Name") + "\n" + "Level " + PlayerPrefs.GetInt("0PlayerLevel").ToString())) {
                     //When Slot 1 already used
                     saveSlot = 0;
                     LoadData ();
                 }
             }else{
                 if (GUI.Button ( new Rect(Screen.width / 2 - 200,205,400,100), "- Empty Slot -")) {
                     //Empty Slot 1
                 }
             }
             //---------------Slot 2 [ID 1]------------------
             if(PlayerPrefs.GetInt("1PreviousSave") > 0){
                 if (GUI.Button ( new Rect(Screen.width / 2 - 200,315,400,100), PlayerPrefs.GetString("1Name") + "\n" + "Level " + PlayerPrefs.GetInt("1PlayerLevel").ToString())) {
                     //When Slot 2 already used
                     saveSlot = 1;
                     LoadData ();
                 }
             }else{
                 if (GUI.Button ( new Rect(Screen.width / 2 - 200,315,400,100), "- Empty Slot -")) {
                     //Empty Slot 2
                 }
             }
             //---------------Slot 3 [ID 2]------------------
             if(PlayerPrefs.GetInt("2PreviousSave") > 0){
                 if (GUI.Button ( new Rect(Screen.width / 2 - 200,425,400,100), PlayerPrefs.GetString("2Name") + "\n" + "Level " + PlayerPrefs.GetInt("2PlayerLevel").ToString())) {
                     //When Slot 3 already used
                     saveSlot = 2;
                     LoadData ();
                 }
             }else{
                 if (GUI.Button ( new Rect(Screen.width / 2 - 200,425,400,100), "- Empty Slot -")) {
                     //Empty Slot 3
                 }
             }
         }
         if(charSelectMenu){
             GUI.Box ( new Rect(Screen.width / 2 - 350,100,700,500), "Select Player");
             if (GUI.Button ( new Rect(Screen.width / 2 + 295,105,30,30), "X")) {
                 loadMenu = false;
                 charSelectMenu = false;
                 menu = true;
             }
             if (GUI.Button ( new Rect(Screen.width / 2 - 285,175,280,373), playerPic[0])) {
                     //Spawn playerPrefab[0]
                     NewGame(0);
             }
             if (GUI.Button ( new Rect(Screen.width / 2 + 35,175,280,373), playerPic[1])) {
                     //Spawn playerPrefab[1]
                     NewGame(1);
             }
         }
     }
     
     void  LoadData (){
         int spawnId = PlayerPrefs.GetInt(saveSlot.ToString() + "ID");
         GameObject respawn = Instantiate(playerPrefab[spawnId] , transform.position , transform.rotation) as GameObject;
         
         respawn.GetComponent<Status>().level = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerLevel");
         respawn.GetComponent<Status>().atk = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerATK");
         respawn.GetComponent<Status>().def = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerDEF");
         respawn.GetComponent<Status>().matk = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMATK");
         respawn.GetComponent<Status>().mdef = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMDEF");
         respawn.GetComponent<Status>().mdef = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMDEF");
         respawn.GetComponent<Status>().exp = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerEXP");
         respawn.GetComponent<Status>().maxExp = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMaxEXP");
         respawn.GetComponent<Status>().maxHealth = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMaxHP");
         respawn.GetComponent<Status>().health = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMaxHP");
         respawn.GetComponent<Status>().maxMana = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMaxMP");
         respawn.GetComponent<Status>().mana = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerMaxMP");    
         respawn.GetComponent<Status>().statusPoint = PlayerPrefs.GetInt(saveSlot.ToString() + "PlayerSTP");
         //-------------------------------
         respawn.GetComponent<Inventory>().cash = PlayerPrefs.GetInt(saveSlot.ToString() + "Cash");
         int itemSize = respawn.GetComponent<Inventory>().itemSlot.Length;
         int a = 0;
         if(itemSize > 0){
             while(a < itemSize){
                 respawn.GetComponent<Inventory>().itemSlot[a] = PlayerPrefs.GetInt(saveSlot.ToString() + "Item" + a.ToString());
                 respawn.GetComponent<Inventory>().itemQuantity[a] = PlayerPrefs.GetInt(saveSlot.ToString() + "ItemQty" + a.ToString());
                 //-------
                 a++;
             }
         }
         
         int equipSize = respawn.GetComponent<Inventory>().equipment.Length;
         a = 0;
         if(equipSize > 0){
             while(a < equipSize){
                 respawn.GetComponent<Inventory>().equipment[a] = PlayerPrefs.GetInt(saveSlot.ToString() + "Equipm" + a.ToString());
                 a++;
             }
         }
         respawn.GetComponent<Inventory>().weaponEquip = 0;
         respawn.GetComponent<Inventory>().armorEquip = PlayerPrefs.GetInt(saveSlot.ToString() + "ArmoEquip");
         if(PlayerPrefs.GetInt(saveSlot.ToString() + "WeaEquip") == 0){
             respawn.GetComponent<Inventory>().RemoveWeaponMesh();
         }else{
             respawn.GetComponent<Inventory>().EquipItem(PlayerPrefs.GetInt(saveSlot.ToString() + "WeaEquip") , respawn.GetComponent<Inventory>().equipment.Length + 5);
         }
         //----------------------------------
         //Load Skill Slot
         a = 0;
         while(a <= 2){
             respawn.GetComponent<SkillWindow>().skill[a] = PlayerPrefs.GetInt(saveSlot.ToString() + "Skill" + a.ToString());
             a++;
         }
         respawn.GetComponent<SkillWindow>().AssignAllSkill();
         DontDestroyOnload dst = respawn.GetComponent<DontDestroyOnload>();
         if(!dst){
             respawn.gameObject.AddComponent<DontDestroyOnload>();
         }
         Time.timeScale = 1.0f;
         Application.LoadLevel(startScene);
         
     }
     
     void NewGame(int id){
         Time.timeScale = 1.0f;
         //Spawn Player from received ID
         GameObject spawn = Instantiate(playerPrefab[id] , transform.position , transform.rotation) as GameObject;
         DontDestroyOnload dst = spawn.GetComponent<DontDestroyOnload>();
         if(!dst){
             spawn.gameObject.AddComponent<DontDestroyOnload>();
         }
         Application.LoadLevel(startScene);
     }
 }

If I find a solution I will post it here, thank you for your time.

Comment

People who like this

0 Show 1
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 HarshadK · Jun 25, 2014 at 02:20 PM 0
Share

As I understand your problem, you need to get the key inputs from the controller and then call the required function as per the keypress. You need to do this inside Update() function.

Refer: Conventional Game Input

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Keyboard/Joystick Inputs Do Not Work until Mouse Clicked 0 Answers

Use Gamepad right analog stick instead of mouse to control crosshair movement 3 Answers

Joystick control instead mouse 0 Answers

"Click" a GUI Button using only joystick input 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