Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
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
Add comment
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

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.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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