Question by
PaincideStudio · Dec 29, 2017 at 07:39 PM ·
buttonplayerprefssave
Save button state using Playerprefs
Hi.
I'm making Android Clicker Game.
I want to save my button state on the DataController GameObject which controls all the data.
Everything works fine except data saving.
I want to make data automatically saved when I exit the game.
Here is the scripts.
Button Script(This controls the button. Also brings and saves the data from and on the DataController)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BGMBuy : MonoBehaviour {
public AudioSource sound;
public string bgmName;
public Button BGM;
public Button Disabled;
public int currentCost;
[HideInInspector]
public int butdisable;
public bool bought;
void Start()
{
DataController.Instance.LoadBGMBuy (this);
bought = false;
}
public void PurchaseMusic ()
{
if (DataController.Instance.gold >= currentCost)
{
if (bought == false)
{
DataController.Instance.gold -= currentCost;
PlayerPrefs.SetInt ("butdisable", 1);
sound.Play ();
BGM.interactable = true;
Disabled.interactable = false;
DataController.Instance.SaveBGMBuy (this);
}
else
{
Disabled.interactable = true;
}
}
}
}
DataController Script(This controls all the data. Every datas are saved here. Other scripts are used to bring the data from here)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DataController : MonoBehaviour {
private static DataController instance;
public static DataController Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<DataController> ();
if (instance == null)
{
GameObject container = new GameObject ("DataController");
instance = container.AddComponent<DataController> ();
}
}
return instance;
}
}
private ItemButton[] itemButtons;
public long gold
{
get
{
if (!PlayerPrefs.HasKey ("Gold"))
{
return 0;
}
string tmpGold = PlayerPrefs.GetString ("Gold");
return long.Parse (tmpGold);
}
set
{
PlayerPrefs.SetString ("Gold", value.ToString());
}
}
public int goldPerClick
{
get
{
return PlayerPrefs.GetInt("GoldPerClick",1);
}
set
{
PlayerPrefs.SetInt ("GoldPerClick", value);
}
}
void Awake()
{
//PlayerPrefs.DeleteAll ();
itemButtons = FindObjectsOfType <ItemButton> ();
}
public void LoadUpgradeButton(UpgradeButton upgradeButton)
{
string key = upgradeButton.upgradeName;
upgradeButton.level = PlayerPrefs.GetInt (key + "_level", 1);
upgradeButton.goldByUpgrade = PlayerPrefs.GetInt (key + "_goldByUpgrade", upgradeButton.startGoldByUpgrade);
upgradeButton.currentCost = PlayerPrefs.GetInt (key + "_cost", upgradeButton.startCurrentCost);
}
public void SaveUpgradeButton(UpgradeButton upgradeButton)
{
string key = upgradeButton.upgradeName;
PlayerPrefs.SetInt (key + "_level", upgradeButton.level);
PlayerPrefs.SetInt (key + "_goldByUpgrade", upgradeButton.goldByUpgrade);
PlayerPrefs.SetInt (key + "_cost", upgradeButton.currentCost);
}
public void LoadItemButton(ItemButton itemButton)
{
string key = itemButton.itemName;
itemButton.level = PlayerPrefs.GetInt(key + "_level");
itemButton.currentCost = PlayerPrefs.GetInt (key + "_cost", itemButton.startCurrentCost);
itemButton.goldPerSec = PlayerPrefs.GetInt (key + "_goldPerSec");
if (PlayerPrefs.GetInt (key + "_isPurchased") == 1)
{
itemButton.isPurchased = true;
}
else
{
itemButton.isPurchased = false;
}
}
public void SaveItemButton(ItemButton itemButton)
{
string key = itemButton.itemName;
PlayerPrefs.SetInt(key + "_level",itemButton.level);
PlayerPrefs.SetInt (key + "_cost", itemButton.currentCost);
PlayerPrefs.SetInt (key + "_goldPerSec",itemButton.goldPerSec);
if (itemButton.isPurchased == true)
{
PlayerPrefs.SetInt (key + "_isPurchased", 1);
}
else
{
PlayerPrefs.SetInt (key + "_isPurchased", 0);
}
}
public int GetGoldPerSec()
{
int goldPerSec = 0;
for (int i = 0; i < itemButtons.Length; i++)
{
goldPerSec += itemButtons [i].goldPerSec;
}
return goldPerSec;
}
public void LoadBGMBuy(BGMBuy bgmBuy)
{
string key = bgmBuy.bgmName;
bgmBuy.butdisable = PlayerPrefs.GetInt (key + "bought");
if(bgmBuy.bought == true)
{
PlayerPrefs.GetInt (key + "_bought",1);
}
else
{
PlayerPrefs.GetInt (key + "_bought",0) ;
}
}
public void SaveBGMBuy(BGMBuy bgmBuy)
{
string key = bgmBuy.bgmName;
PlayerPrefs.SetInt (key + "_bought", bgmBuy.butdisable);
if(bgmBuy.bought == true)
{
PlayerPrefs.SetInt (key + "_bought",1);
}
else
{
PlayerPrefs.SetInt (key + "_bought",0) ;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Save Button state(if shown or hidden) 0 Answers
Save/load not work on mobile phone 1 Answer
Playerprefs, i dont know how to use 0 Answers
Save and load component values 0 Answers
PlayerPrefs Autosave [SOLVED] 2 Answers