- Home /
Adding only once a new Value to a saved int List
In my game you can choose from different Avatars (Scriptable Objects). However, most of the Avatars are locked and need to be unlocked in the game. I save the unlocked Avatars in a List of my Data Manager. My Problem: How can I manually add new Avatars IDs (int values) to the List so the next time when I start the Game, a new unlocked Avatar is available? Right now I have a method called "UnlockAvatar" in the DataManager script which perfectly saves the ID of the new unlocked Avatar into the List of unlocked Avatars. This method is called in the Avatar Selection Menu when you purchase a new Avatar. I need it for the following scenario: in the next game update I will add a new Avatar which will have the new ID "100" BUT which is unlocked by default. I know that the easiest way would be to simply call the following method in the GameManager Start() Method:
DataManager.UnlockAvatar(100);
But that way this method will be called every time the player starts the game, there must be a simpler solution, right?
This is my User Data Script:
[Serializable]
public class UserData
{
public List<int> unlockedAvatarIDs;
public UserData()
{
unlockedAvatarIDs = new List<int>();
unlockedAvatarIDs.Add(0); //the only unlocked Avatar when playing for the first time
}
}
And here comes my Data Manager Script:
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEditor;
using UnityEngine;
public static class DataManager
{
public static void UnlockAvatar(int index)
{
UserData userData = Load();
foreach (var item in userData.unlockedAvatarIDs)
{
if (item == index)
{
return; //Don't save if index is already saved in the list
}
}
userData.unlockedAvatarIDs.Add(index);
Save(userData);
}
public static List<int> GetIDsOfUnlockedAvatars()
{
UserData userData = Load();
return userData.unlockedAvatarIDs;
}
/// <summary>
/// Sava user data in binary format
/// </summary>
/// <param name="data"></param>
private static void Save(UserData data)
{
string path = GetDataFilePath();
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = File.Open(path, FileMode.OpenOrCreate))
{
binaryFormatter.Serialize(fileStream, data);
}
}
/// <summary>
/// Load user data
/// </summary>
/// <returns></returns>
public static UserData Load()
{
string path = GetDataFilePath();
if (!File.Exists(path))
{
UserData userData = new UserData();
Save(userData); //To create file if file doesn't exists
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = File.Open(path, FileMode.Open))
{
return (UserData)binaryFormatter.Deserialize(fileStream);
}
}
}
The only location where an Avatar is unlocked is the "Avatar Selection Page". Here is the script attached to the Page: using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.PlayerLoop; using UnityEngine.UI;
public class ChooseAvatar : MonoBehaviour, IEndDragHandler, IDragHandler
{
[SerializeField]
private GameObject unlockButton;
[SerializeField]
private List<Avatar> avatars;
private int browsingIndex = 0; //browsingIndex gets updated based on the shown avatar on page
private void OnEnable()
{
LoadAvatarLockStatus();
}
private void LoadAvatarLockStatus()
{
List<int> defaultUnlockedAvatarIDs = DataManager.GetIDsOfUnlockedAvatars();
foreach (var item in avatars)
{
item.Locked = defaultUnlockedAvatarIDs.Contains(item.RocketID) ? false : true;
}
}
public void PurchaseUnlock()
{
DataManager.UnlockAvatar(avatars[browsingIndex].RocketID); // Saving the ID of unlocked avatar
}
}
I would appreciate any help!!!