- Home /
Android, How can I permanently save data? (A couple variables)
Hi, I have made a game which the user can get a score from and their high score is kept in playerPrefs, I also use playerPrefs to record if the user has purchased "no ads" which I assumed would always save the data but earlier I discovered that if the user uninstalls the application and re-installs it then the playerPrefs are lost.
How can I save this data even when the application has been uninstalled?
I would not recommend that. I mean, from a user perspective I would not like apps to leave their data after i uninstall them.
The proper way to do that is store the relevant data to a server, and upon starting the application if no local data is present, retrieve it from the server.
Thanks for replying, if I was gonna go down the server route then how would I go about this?
Well, for the local storage, playerprefs works just fine. I would add encryption since the data is susceptible to editing. As for the remote option, you're practically free to go any way you want. You would need some sort of DB+Authentication to store data for multiple users.
For the 'first-run' logic i would suggest checking for local data to be the condition. If no local data is found (key in the playerprefs) then it's your first run and check with the server to get it.
Answer by sindrijo · Jun 30, 2016 at 05:19 PM
You can save arbitrary strings that is tied to a users google play game services identity, so that it will be persistent for the user even if the user switches devices. The tricky part is how to sync the data between them. CloudOnce is a totally free asset with that makes this a breeze.
Check out the getting started page: http://trollpants.github.io/CloudOnce/gettingStarted.html
You may notice that it also supports iOS and helps with things like achievements and leaderboards as well.
Answer by DroidifyDevs · Jun 30, 2016 at 01:44 PM
You'd probably have to post the data to a server.
However you could try serialization... but I'm not sure if that will save after an uninstall.
This is what I use to serialize a dictionary, feel free to try it out:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System;
using System.Collections.Generic;
//this is SaveInfoNewer.cs
//this script saves and loads all the info we want
public class SaveInfoNewer : MonoBehaviour
{
public SlotLoader SlotLoader;
//data is what is finally saved
public Dictionary<string, int> data;
void Awake()
{
//LoadUpgradeData();
LoadData();
//WARNING! data.Clear() deletes EVERYTHING
//data.Clear();
//SaveData();
}
public void LoadData()
{
//this loads the data
data = SaveInfoNewer.DeserializeData<Dictionary<string, int>>("PleaseWork.save");
}
public void SaveData()
{
//this saves the data
SaveInfoNewer.SerializeData(data, "PleaseWork.save");
}
public static void SerializeData<T>(T data, string path)
{
//this is just magic to save data.
//if you're interested read up on serialization
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, data);
Debug.Log("Data written to " + path + " @ " + DateTime.Now.ToShortTimeString());
}
catch (SerializationException e)
{
Debug.LogError(e.Message);
}
finally
{
fs.Close();
}
}
public static T DeserializeData<T>(string path)
{
//this is the magic that deserializes the data so we can load it
T data = default(T);
if (File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
data = (T)formatter.Deserialize(fs);
Debug.Log("Data read from " + path);
}
catch (SerializationException e)
{
Debug.LogError(e.Message);
}
finally
{
fs.Close();
}
}
return data;
}
}
In theory this should save the data even after uninstalling, but only way to find out is to try it out.
Answer by EDevJogos · Jun 30, 2016 at 02:09 PM
I would suggest using a database for this, you can use kii it's free and has native support for unity, quite simple to implement too, just follow the getting started tutorial:
https://docs.kii.com/en/guides/unity/
This page has some sample cods, as you can see it's really simple:
Answer by rmassanet · Jul 13, 2016 at 12:09 PM
You could use Firebase from Google. They have a Unity plugin for it:
https://github.com/firebase/Firebase-Unity
That way you can store data in their cloud, associated to the user's Google Play Game's account.
Good luck!