A
A
Alexander2017-10-27 20:38:02
C++ / C#
Alexander, 2017-10-27 20:38:02

How to make save in c#, unity3d?

public class Game : MonoBehaviour {
public int score;
public int bonus;

<i>public int[] costUpgrades;
public Text[] textUpgrades;</i>

private Save save = Save();

private string path;

void Awake ()
{
path = Path.Combine(Application.dataPath, "Save.json");
if (File.Exists (path)) {
save = JsonUtility.FromJson<Save> (File.ReadAllText (path));
score = save.score;
bonus = save.bonus;
}

void OnApplicationQuit ()
{
File.WriteAllText (path, JsonUtility.ToJson (save));
}

void Upgrade1 (int index)
{
if(score >= costUpgrades[index])
{
score -= costUpgrades[index];
...

}
}

void OnClick ()
{
score += bonus;
save.score = score;
}
}

[Serializable]
public class Save 
{
public int bonus;
public int score;
public int[] costUpgrades;
public Text[] textUpgrades;
}

You need to save the cost of the upgrade (costUpgrades) and the text (textUpgrades). If everything is clear with the score and the bonus: save.score = score;
then the array, unfortunately, I do not understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheTalion, 2017-10-27
@zernvaka

An array of text is wrong. Text is a component. You need to take it like this:

Text uiText;
string text;

text = uiText.text; (или как-то так)

And serialize an array of strings and everything will be fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question