Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

About Playerprefs Save & Load

$
0
0
Hi.
I'm currently making 2d clicker game.
I'm having issue with Playerprefs.
What I'm trying to do is " Click [button A] and when [button A] is clicked, it becomes disabled and [button B] becomes enabled. When I quit my game, I want the button state saved "
Everything works fine except saving.
My game is played on the android, and it always saves when I quit my game.
But when I click my buttons and found them work good, it doesn't save the data.
I'm saving my data on the DataController(Which I made with Empty game object).
BGM Buying button script 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 ( You need to look at LoadBGMBuy ~ SaveBGMBuy at the below. Everything works fine except that part) 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 (); if (instance == null) { GameObject container = new GameObject ("DataController"); instance = container.AddComponent (); } } 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 (); } 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) ; } } }

Viewing all articles
Browse latest Browse all 1333

Trending Articles