[Editor Utility] Player Prefs Editor - Edit Player Prefs inside the Unity editor!

Hello everyone! I wrote this quick editor for one of my personal projects by I decided to share it with you guys! This little utility is very useful for people who need to use the PlayerPrefs class to save information. This tool allows you to access, modify and delete elements of the save file without digging through your system and finding the file to edit! Here’s how it works:

To open the editor

  • Install the file
  • Click Edit>Player Prefs

To get a value

  • Change “Key to Set” to the key you want to retrieve
  • Change the Field type to the type of key you are storing
  • Press “Get Key”. The value will be pasted into the value field

To set a value
PLEASE NOTE: This option will erase any previous value in the preferences file. There is no way to restore it!

  • Change “Key to Set” to the key you want to set.
  • Change the Field type to the type of key you want to store
  • Enter a new value for the key
  • Press “Set Key”. The value will be saved into the Player Prefs file.

To delete a value
PLEASE NOTE: This option will erase the value in the preferences file. There is no way to restore it!

  • Change “Key to Set” to the key you want to delete.
  • Press “Delete Key”. The value will be erased from the Player Prefs file.

If you need to as well, there is an option to delete all keys stored in the Player Prefs file! Be careful, because this will also erase the information from the Unity setup dialog (the box that shows the screen resolution, graphics settings, controls, etc).

Here’s the full source code, just drop into a C# file in your /Assets/Editor folder, and call the file “PlayerPrefsEditor.cs”

using UnityEngine;
using UnityEditor;
using System.Collections;

public class PlayerPrefsEditor : EditorWindow {

    [MenuItem("Edit/Player Prefs")]
    public static void openWindow() {

        PlayerPrefsEditor window = (PlayerPrefsEditor)EditorWindow.GetWindow(typeof(PlayerPrefsEditor));
        window.titleContent = new GUIContent("Player Prefs");
        window.Show();

    }

    public enum FieldType { String,Integer,Float }

    private FieldType fieldType = FieldType.String;
    private string setKey = "";
    private string setVal = "";
    private string error = null;

    void OnGUI() {

        EditorGUILayout.LabelField("Player Prefs Editor", EditorStyles.boldLabel);
        EditorGUILayout.LabelField("by RomejanicDev");
        EditorGUILayout.Separator();

        fieldType = (FieldType)EditorGUILayout.EnumPopup("Key Type", fieldType);
        setKey = EditorGUILayout.TextField("Key to Set", setKey);
        setVal = EditorGUILayout.TextField("Value to Set", setVal);

        if(error != null) {

            EditorGUILayout.HelpBox(error, MessageType.Error);

        }

        if(GUILayout.Button("Set Key")) {

            if(fieldType == FieldType.Integer) {

                int result;
                if(!int.TryParse(setVal, out result)) {
                   
                    error = "Invalid input \"" + setVal + "\"";
                    return;
                   
                }
               
                PlayerPrefs.SetInt(setKey, result);

            } else if(fieldType == FieldType.Float) {

                float result;
                if(!float.TryParse(setVal, out result)) {

                    error = "Invalid input \"" + setVal + "\"";
                    return;

                }

                PlayerPrefs.SetFloat(setKey, result);

            } else {

                PlayerPrefs.SetString(setKey, setVal);

            }

            PlayerPrefs.Save();
            error = null;

        }

        if(GUILayout.Button("Get Key")) {
       
            if(fieldType == FieldType.Integer) {

                setVal = PlayerPrefs.GetInt(setKey).ToString();

            } else if(fieldType == FieldType.Float) {

                setVal = PlayerPrefs.GetFloat(setKey).ToString();

            } else {

                setVal = PlayerPrefs.GetString(setKey);

            }
       
        }

        if(GUILayout.Button("Delete Key")) {

            PlayerPrefs.DeleteKey(setKey);
            PlayerPrefs.Save();

        }

        if(GUILayout.Button("Delete All Keys")) {

            PlayerPrefs.DeleteAll();
            PlayerPrefs.Save();

        }

    }

}

Hope you enjoy the editor! It is really handy if you need to make quick adjustments to the PlayerPrefs file without writing a new script or editing the file directly! If you make any modifications and improvements to this code, please leave credit to me, and leave the “by Romejanic” label in the GUI code!

Thanks!
Romejanic

16 Likes

Teşekkürler. Çok işime yaradı.

Thank you very much! This is going to help out a lot :).

OMG, this code helped me so much. Thank you!

Very good !

Works like a charm. Thanks!

OMG, thank you so much, It would be amazing if there is a way to get all keys xD

Cheers to you, Romejanic! Quite useful.

Thanks

Thanks for sharing!

what a great editor script–thanks!

That's a really useful editor script!
With this small modification you don't need to tell it what to look for when using the Get Key option.

        if (GUILayout.Button("Get Key"))
        {
            error = null;
            setVal = "-";
            int i;
            float f;
            string s;

            if (PlayerPrefs.HasKey(setKey))
            {
                i = PlayerPrefs.GetInt(setKey, int.MinValue);
                if (i == int.MinValue)
                {
                    f = PlayerPrefs.GetFloat(setKey, float.NaN);
                    if (float.IsNaN(f))
                    {
                        s = PlayerPrefs.GetString(setKey, string.Empty);
                        if (string.IsNullOrEmpty(s))
                        {
                            error = "Unknown type: " + setKey;
                        }
                        else
                        {
                            setVal = s.ToString();
                            fieldType = FieldType.String;
                        }
                    }
                    else
                    {
                        setVal = f.ToString();
                        fieldType = FieldType.Float;
                    }
                }
                else
                {
                    setVal = i.ToString();
                    fieldType = FieldType.Integer;
                }
            }
            else
            {
                error = "No matching Key " + setKey;
            }
        }
2 Likes

Thanks so much..

Thank you <3.

Great script! Thanks very much!

Great tool!

Amazing, this works great! You rock

It is 2022, and still awesome tool, thank you a lot!