- Home /
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.
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
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
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
Keyboard/Joystick Inputs Do Not Work until Mouse Clicked 0 Answers