- Home /
Question about my Upgrade System with PlayerPrefs
Hi guys,
I have made an Upgrade System for my game with UI Buttons and so on. I try to save the buttons with PlayerPrefs to keep them deactivated if the player has activated/bought the upgrade. The problem right now is that I have many Upgrades and I think the way how I save them isn't good enough or rather to complicated.
Have you guys other or better solutions for this or how would you do it? The game is for mobile devices.
Here is the snippet script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UpgradeManager : MonoBehaviour {
public Button speed1;
public Button speed2;
public GameObject lockedSpeed2;
public Button fuel1;
public Button fuel2;
public GameObject lockedFuel2;
private float speed;
private float fuel;
private float speedUpgrade;
private float fuelUpgrade;
void Awake ()
{
speedUpgrade = PlayerPrefs.GetInt ("SavedSpeedUpgrade");
fuelUpgrade = PlayerPrefs.GetInt ("SavedFuelUpgrade");
}
void Start () {
if ( speedUpgrade == 1)
{
speed1.enabled = false;
speed2.enabled = true;
lockedSpeed2.SetActive(false);
}
if ( speedUpgrade == 2)
{
speed1.enabled = false;
speed2.enabled = false;
lockedSpeed2.SetActive(false);
}
if ( fuelUpgrade == 1)
{
fuel1.enabled = false;
fuel2.enabled = true;
lockedFuel2.SetActive(false);
}
if ( fuelUpgrade == 2)
{
fuel1.enabled = false;
fuel2.enabled = false;
lockedFuel2.SetActive(false);
}
}
//Speed Upgrade
public void Speed1()
{
speed1.transition = Selectable.Transition.None;
speed = 15;
Debug.Log ("+15 Speed");
PlayerPrefs.SetFloat ("Speed", speed);
PlayerPrefs.SetInt("SavedSpeedUpgrade", 1);
lockedSpeed2.SetActive(false);
speed1.enabled = false;
}
public void Speed2()
{
speed2.transition = Selectable.Transition.None;
speed = 20;
Debug.Log ("+20 Speed");
PlayerPrefs.SetFloat ("Speed", speed);
PlayerPrefs.SetInt("SavedSpeedUpgrade", 2);
speed2.enabled = false;
}
//Fuel Upgrade
public void Fuel1()
{
fuel1.transition = Selectable.Transition.None;
fuel = 5;
Debug.Log ("+5 Fuel");
PlayerPrefs.SetFloat ("Fuel", fuel);
PlayerPrefs.SetInt("SavedFuelUpgrade", 1);
lockedFuel2.SetActive(false);
fuel1.enabled = false;
}
public void Fuel2()
{
fuel2.transition = Selectable.Transition.None;
fuel = 10;
Debug.Log ("+10 Fuel");
PlayerPrefs.SetFloat ("Fuel", fuel);
PlayerPrefs.SetInt("SavedFuelUpgrade", 2);
fuel2.enabled = false;
}
}
Comment