- Home /
saven data files (Units, Gold, Items, unlocked levels) on devices
hey there i am developing a mobile game and reached the point where i have to figure out how to save the data of the players. like, how much golg, what units and so on... i heard, playerprefs are not good, because they dont encrypt or something..? so i would like to know what is a good solution, what are other studios using? i would love to hear some different possibilities and why they are good :)
oh yeah, it will be an offline game, but maybe i want to make later a highscore or add an multiplayer..
thank you much !
Answer by MT369MT · May 20, 2018 at 06:17 PM
Hi, I use custom binary files for saves. If you search "Saving Data in Unity" on youtube you will find many saving options like PlayerPrefs, Custom Binary Files, XML Files, Scriptable Objects and in each there are explained the pros and cons. Ask if you need something more.
"custom binary files" sound complicated for me :p but i will check out youtube for it.
are custom binary files fine for an offline game on mobile? or is this way to much and there are other options? what would you use?
This is the complete script of the Custom Binary File (I modified it a bit). Add it to an empty gameobject called "SaveControl" and then add also the second script in the scene. If you change the CurrentLevel value (in play mode use up and down arrow to modify it) and then you exit play mode and restart it, you will see that the int will remain the previous value.
SaveControl script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class SaveControl : $$anonymous$$onoBehaviour {
public static SaveControl Control;
public bool CanLoad;
public bool Loaded;
public float LoadCounter;
void Awake()
{
if (Control == null)
{
DontDestroyOnLoad(gameObject);
Control = this;
}
else if (Control != this)
{
Destroy(gameObject);
}
}
void Start()
{
CanLoad = true;
Loaded = false;
}
void Update()
{
if (CanLoad == true)
{
LoadCounter += Time.deltaTime;
if (LoadCounter >= 0.1)
{
Load();
LoadCounter = 0;
CanLoad = false;
}
}
}
private void OnApplicationQuit()
{
if (Loaded == true)
{
Save();
}
}
public void Save()
{
BinaryFormatter BF = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData Data = new PlayerData();
//I save my CurrentLevel int, in my data file
Data.CurrentLevel = Level.CurrentLevel;
BF.Serialize(file, Data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter BF = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", File$$anonymous$$ode.Open);
PlayerData Data = (PlayerData)BF.Deserialize(file);
file.Close();
//I load in my CurrentLevel int, my data that I previous stored
Level.CurrentLevel = Data.CurrentLevel;
Loaded = true;
}
}
}
[Serializable] class PlayerData {
public int CurrentLevel;
}
Level script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Level : $$anonymous$$onoBehaviour {
public static int CurrentLevel;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown("up"))
{
CurrentLevel += 1;
}
if (Input.Get$$anonymous$$eyDown("down"))
{
CurrentLevel -= 1;
}
Debug.Log(CurrentLevel);
}
}
allright, this is quit interessting. do it saves more than 1 mb? (like playerprefs) or can you save as much data as you want?
oh and btw, the tutorials are very good u told me to watch, thanks for that.