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;
}
}
Answer by MaxOR_PrimE · Oct 14, 2020 at 07:34 AM
check this tutorial Save & Load
i see already this tuto but it's not the same case ( i'm a beginner )
what type of saving you try to do ?
Answer by unity_jQdsvObzs6zEJw · Oct 14, 2020 at 07:37 PM
playerprefs
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
Follow this Question
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