I have already developed a code that when i press a button it unlock a level. But for example if i unlock level 5, all the others levels <5 are also unlocked (The level 2, 3, 4).
public class ButtonSettings : MonoBehaviour {
public static int releasedLevelStatic = 1;
public int releasedLevel;
public string nextLevel;
void Awake(){
if (PlayerPrefs.HasKey ("Level")) {
releasedLevelStatic = PlayerPrefs.GetInt ("Level", releasedLevelStatic);
}
}
public void ButtonNextLevel(){
if (releasedLevelStatic <= releasedLevel) {
releasedLevelStatic = releasedLevel;
PlayerPrefs.SetInt ("Level", releasedLevelStatic);
}
}
Then i have another code to attach at some buttons:
public class LevelManager : MonoBehaviour {
public int level;
public Image Cadeado;
private string LevelString;
void Start () {
if (ButtonSettings.releasedLevelStatic >= level) {
LevelUnlocked ();
} else {
LevelLocked ();
}
}
public void LevelSelect(string _level){
LevelString = _level;
SceneManager.LoadScene (LevelString);
}
void LevelLocked(){
GetComponent<Button> ().interactable = false;
Cadeado.enabled = true;
}
void LevelUnlocked(){
GetComponent<Button> ().interactable = true;
Cadeado.enabled = false;
}
}
So the think i want to do is, if i unlock card 7, the card nrº 7 will be unlocked but all the cards <7 (1, 2, 3, 4, 5, 6) should not be unlocked.
Well you could try making a collection that would contain unlocked card levels. And save that collection of numbers in a form of a string using PlayerPrefs.SetString method.
Here’s a small sample:
using System.Collections.Generic;
public static class SaveData
{
public static IReadOnlyCollection<int> UnlockedCardLevels => unlockedCardLevels as IReadOnlyCollection<int>;
private static HashSet<int> unlockedCardLevels = new HashSet<int>();
public static void Unlock(int cardLevel) => unlockedCardLevels.Add(cardLevel);
public static void Lock(int cardLevel) => unlockedCardLevels.Remove(cardLevel);
public static void Clear() => unlockedCardLevels.Clear();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
public static class SaveSystem
{
private const string CardLevelKey = "CardLevel";
public static void SaveUnlockedCards()
{
PlayerPrefs.SetString(CardLevelKey, UnlockedCardsAsString());
}
public static void LoadUnlockedCards()
{
if (!PlayerPrefs.HasKey(CardLevelKey))
{
return;
}
string cardData = PlayerPrefs.GetString(CardLevelKey);
foreach (int cardLevel in UnlockedCardsFromString(cardData))
{
SaveData.Unlock(cardLevel);
}
}
public static HashSet<int> UnlockedCardsFromString(string cardData)
{
HashSet<int> unlockedCards = new HashSet<int>();
if (cardData == string.Empty)
{
return unlockedCards;
}
foreach (string cardLevelString in cardData.Split(' '))
{
if (int.TryParse(cardLevelString, out int cardLevel))
{
unlockedCards.Add(cardLevel);
}
else
{
Debug.LogError("Error in reading save data!");
}
}
return unlockedCards;
}
public static string UnlockedCardsAsString()
{
if (SaveData.UnlockedCardLevels.Count == 0)
{
return string.Empty;
}
StringBuilder builder = new StringBuilder();
builder.Append(SaveData.UnlockedCardLevels.First());
foreach (int cardLevel in SaveData.UnlockedCardLevels.Skip(1))
{
builder.Append(' ');
builder.Append(cardLevel);
}
return builder.ToString();
}
}
using System.Linq;
using UnityEngine;
public class SaveSystemTester : MonoBehaviour
{
private void Start()
{
SaveSystem.LoadUnlockedCards();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
ToggleCardStatus(1);
else if (Input.GetKeyDown(KeyCode.Alpha2))
ToggleCardStatus(2);
else if (Input.GetKeyDown(KeyCode.Alpha3))
ToggleCardStatus(3);
else if (Input.GetKeyDown(KeyCode.Alpha4))
ToggleCardStatus(4);
else if (Input.GetKeyDown(KeyCode.Alpha5))
ToggleCardStatus(5);
else if (Input.GetKeyDown(KeyCode.Alpha6))
ToggleCardStatus(6);
else if (Input.GetKeyDown(KeyCode.Alpha7))
ToggleCardStatus(7);
else if (Input.GetKeyDown(KeyCode.Alpha8))
ToggleCardStatus(8);
else if (Input.GetKeyDown(KeyCode.Alpha9))
ToggleCardStatus(9);
else if (Input.GetKeyDown(KeyCode.F))
PrintUnlockedCards();
}
private void ToggleCardStatus(int level)
{
if (SaveData.UnlockedCardLevels.Contains(level))
{
SaveData.Lock(level);
}
else
{
SaveData.Unlock(level);
}
}
private void PrintUnlockedCards()
{
Debug.Log(SaveSystem.UnlockedCardsAsString());
}
private void OnApplicationQuit()
{
SaveSystem.SaveUnlockedCards();
}
}