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?

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.

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.

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:

https://docs.kii.com/en/samples/Gamecloud-Unity/

You could use Firebase from Google. They have a Unity plugin for it:

That way you can store data in their cloud, associated to the user’s Google Play Game’s account.

Good luck!