• 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 unity_jQdsvObzs6zEJw · Oct 13, 2020 at 12:11 PM · playerprefssave datainventory system

how I can save the data of a list of purchased items ?

I want to save the list of items purchased from a store and displayed in another list here is the store script : using UnityEngine; using UnityEngine.UI; using System.Collections.Generic;

[System.Serializable]

public class Shop : MonoBehaviour { #region Singlton:Shop

 public static Shop Instance;

 void Awake()
 {
     if (Instance == null)
         Instance = this;
     //else
     //    Destroy (gameObject);
 }

 #endregion

 [System.Serializable] public class ShopItem
 {
     public Sprite Image;
     public int Price;
     
     public bool IsPurchased = false;




     public static void SetBool(string IsPurchased, bool state)
     {
         PlayerPrefs.SetInt(IsPurchased, state ? 1 : 0);
     }

      public static bool GetBool(string IsPurchased)
     {
         int value = PlayerPrefs.GetInt(IsPurchased);

         if (value == 1)
         {
             return true;
         }

         else
         {
             return false;
         }
     }
 }

 public List<ShopItem> ShopItemsList;
 public List<ShopItem> ShopItemsListIcon;
 [SerializeField] Animator NoCoinsAnim;
 

 [SerializeField] GameObject ItemTemplate;
 GameObject g;
 [SerializeField] Transform ShopScrollView;
 
 Button buyBtn;

 void Start ()
 {
     int len = ShopItemsList.Count;
     for (int i = 0; i < len; i++) {
         g = Instantiate (ItemTemplate, ShopScrollView);
         g.transform.GetChild (0).GetComponent <Image> ().sprite = ShopItemsListIcon [i].Image;
         g.transform.GetChild (1).GetChild (0).GetComponent <Text> ().text = ShopItemsList [i].Price.ToString ();
         buyBtn = g.transform.GetChild (2).GetComponent <Button> ();
         if (ShopItemsList [i].IsPurchased) {
             DisableBuyButton ();
         }
         buyBtn.AddEventListener (i, OnShopItemBtnClicked);
     }
 }

 void OnShopItemBtnClicked (int itemIndex)
 {
     if (Game.Instance.HasEnoughCoins (ShopItemsList [itemIndex].Price)) {
         Game.Instance.UseCoins (ShopItemsList [itemIndex].Price);
         //purchase Item
         ShopItemsList [itemIndex].IsPurchased = true;

         //disable the button
         buyBtn = ShopScrollView.GetChild (itemIndex).GetChild (2).GetComponent <Button> ();
         DisableBuyButton ();
         //change UI text: coins
         Game.Instance.UpdateAllCoinsUIText ();

         //add avatar
         Profile.Instance.AddAvatar (ShopItemsList [itemIndex].Image);
     } else {
         NoCoinsAnim.SetTrigger ("NoCoins");
         Debug.Log ("You don't have enough coins!!");
     }
 }

 void DisableBuyButton ()
 {
     buyBtn.interactable = false;
     buyBtn.transform.GetChild (0).GetComponent <Text> ().text = "PURCHASED";
 }
 

}

Here is the profile script ( where the purchased items is displayed ) :

using UnityEngine; using UnityEngine.UI; using System.Collections.Generic;

public class Profile : MonoBehaviour { #region Singlton:Profile

 public static Profile Instance;

 void Awake ()
 {
     if (Instance == null)
         Instance = this;
     else
         Destroy (gameObject);
 }

 #endregion

 public class Avatar
 {
     public Sprite Image;
 }

 public List<Avatar> AvatarsList;

 [SerializeField] GameObject AvatarUITemplate;
 [SerializeField] Transform AvatarsScrollView;

 GameObject g;
 int newSelectedIndex, previousSelectedIndex;

 [SerializeField] Color ActiveAvatarColor;
 [SerializeField] Color DefaultAvatarColor;

 [SerializeField] Image CurrentAvatar;
 

 void Start ()
 {
     GetAvailableAvatars ();
     newSelectedIndex = previousSelectedIndex = 0;
 }

 void GetAvailableAvatars ()
 {
     for (int i = 0; i < Shop.Instance.ShopItemsList.Count; i++) {
         if (Shop.Instance.ShopItemsList [i].IsPurchased) {
             //add all purchased avatars to AvatarsList
             AddAvatar (Shop.Instance.ShopItemsList [i].Image);
         }
     }

     SelectAvatar (newSelectedIndex);
 }

 public void AddAvatar (Sprite img)
 {
     if (AvatarsList == null)
         AvatarsList = new List<Avatar> ();
     
     Avatar av = new Avatar (){ Image = img };
     //add av to AvatarsList
     AvatarsList.Add (av);

     //add avatar in the UI scroll view
     g = Instantiate (AvatarUITemplate, AvatarsScrollView);
     g.transform.GetChild (0).GetComponent <Image> ().sprite = av.Image;

     //add click event
     g.transform.GetComponent <Button> ().AddEventListener (AvatarsList.Count - 1, OnAvatarClick);
 }

 void OnAvatarClick (int AvatarIndex)
 {
     SelectAvatar (AvatarIndex);
 }

 void SelectAvatar (int AvatarIndex)
 {
     previousSelectedIndex = newSelectedIndex;
     newSelectedIndex = AvatarIndex;
     AvatarsScrollView.GetChild (previousSelectedIndex).GetComponent <Image> ().color = DefaultAvatarColor;
     AvatarsScrollView.GetChild (newSelectedIndex).GetComponent <Image> ().color = ActiveAvatarColor;

     //Change Avatar
     CurrentAvatar.sprite = AvatarsList [newSelectedIndex].Image;
 }


}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MaxOR_PrimE · Oct 14, 2020 at 07:34 AM

check this tutorial Save & Load

Comment
Add comment · Show 2 · Share
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
avatar image unity_jQdsvObzs6zEJw · Oct 14, 2020 at 12:11 PM 0
Share

i see already this tuto but it's not the same case ( i'm a beginner )

avatar image MaxOR_PrimE unity_jQdsvObzs6zEJw · Oct 14, 2020 at 06:29 PM 0
Share

what type of saving you try to do ?

avatar image
0

Answer by unity_jQdsvObzs6zEJw · Oct 14, 2020 at 07:37 PM

playerprefs

Comment
Add comment · Show 1 · Share
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
avatar image MaxOR_PrimE · Oct 15, 2020 at 05:41 AM 0
Share

Ok so the tutorial its i sent to you i just perfect you just have to create a script that change all you variable "data" (playerPref) to type that can be save. You can save all playerPref and much more.. well its work perfectly for me i can save all my playerPref, iventory, skills, life, score event a base you build in my map...

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

218 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 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

PlayerPrefs help ! 1 Answer

Saving In Between Scenes and Program Exit 0 Answers

Convert my playerpref back to obj 1 Answer

Saving last runs score, only adds the previous score to it. 0 Answers

PlayerPrefs not saving specific values 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges