Saving and Loading data in Unity – PlayerPrefs

So, I have been really frustrated with the PlayerPrefs system in unity, and there is a couple of reasons, but first and foremost, if you want to save and load data in Unity games, there is really no way around this, unless you are planning on only releasing on one platform. The second reason I do not like it, is that it is rather problematic to work with, in the sense that you have to remember what you called the saved objects, and this is handled by strings., and to me this is really problematic, because it will make your code look like shit in a matter of minutes after you started using this.

So, I have been thinking about how I can solve this problem, so that I can easily get the list of save and load objects that you have used, and at the same time get easy access to the saved values throughout the game.

My solution is the PrefsControl.cs script – and although, some of you might think.. thats stupid and not new at all.. I am still posting this in case someone in some distant future, who are my equal in a dense mind, can benefit from this. The script follows this form.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PrefsControl : MonoBehaviour {

    //Setting a float PlayerPref. 
    //It is important to note that the savename is set here, and nowhere else,
    //that is the whole purpose of this script.
    public void SetFloatPlayerPrefs(float value){
        PlayerPrefs.SetFloat("savename", value);
    }

    //I always set a default value here, in case that the PlayerPref have not been set
    //so that it will always work.
    public float GetFloatPlayerPrefs(){
        return PlayerPrefs.GetFloat("savename", 0)); 
    }

    //I do the same with all the other types as well. String, Int and Bool. Each 
    //value will have their own Get/Set function.
}

 

I have generally in all of my scenes a GameObject called GameController, this GameObject is tagged with “GameController” and I use this Object for all things that are generic for the game to work. On this object I place the script called “PrefsControl.cs” so that I can easily access it from anywhere in my game.

GameObject gameController = GameObject.FindGameObjectWithTag("GameController");

This also means that I can now easily find the PrefsControl script and do whatever I want with it.

PrefsControl pc = gameController.GetComponent<PrefsControl>();
pc.SetFloatPlayerPrefs(5.0f);

Or I can get it from anywhere

float theFloatValue = pc.GetFloatPlayerPrefs();

At last, I can also simply get this with a “one-liner”

GameObject.FindGameObjectWithTag("GameController").GetComponent<PrefsControl>().SetFloatPlayerPref(5.0f);

This will make saving and loading on a long track, be less messy, and if you are using MonoDevelop or Visual Studio – (or most other serious coding applications) autocomplete will help you immensely.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.