Answer the question
In order to leave comments, you need to log in
How to create a save using PlayerPrefs?
When creating, the developer who explains the principle of saving refers to the List:
public List<Item> shopItems = new List<Item>();
private Save sv = new Save();
sv = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("SV"));
score = sv.score;
for (int i = 0; i < shopItems.Count; i++)
{
shopItems[i].levelOfItem = sv.levelOfItem[i];
shopItems[i].bonusCounter = sv.bonusCounter[i];
if (shopItems[i].needCostMultiplier) shopItems[i].cost *= (int)Mathf.Pow(shopItems[i].costMultiplier, shopItems[i].levelOfItem);
if (shopItems[i].levelOfItem != 0) scoreIncrease += (int)Mathf.Pow(shopItems[i].bonusIncrease, shopItems[i].levelOfItem);
public class Save
{
public int score;
public int[] levelOfItem;
public int[] bonusCounter;
}
public int score;
public int bonus = 1;
public int WorkersCount;
public Save SaveGame = new Save();
private void OnApplicationQuit()
{
SaveGame.score = score;
SaveGame.WorkersCount = new int [WorkersCount.Count];
for (int i = 0; i < WorkersCount.Count; i++)
{
SaveGame.WorkersCount[i] = WorkersCount[i].WorkersCount;
}
PlayerPrefs.SetString("GameSave", JsonUtility.ToJson(SaveGame));
}
[Serializable]
public class Save
{
public int score;
public int [] WorkersCount;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question